Learn Python List Data Structure – Part 1

Data Structure is a collection of data types, the relationship among them and the functions or operations that can be applied on the data. Data type can be string, Integer, Floating value and so on.

What is the difference between Mutable/Immutable object?
Mutable Objects
  1. Objects whose state can be changed once it is created like adding, updating or deleting elements.
  2. Lists, Dictionary, Set, bytearray are mutable object types in python.
Immutable Objects
  1. Object state cannot be modified. Once the object is created, we cannot add, remove or update elements.
  2. String, Integer, Tuples, Frozenset are some of the immutable object types in python.
What is the difference between Homogeneous/Heterogeneous data structure?
  1. Homogeneous Data Structure – Data elements will be of same data type (ex: Array).
  2. Heterogeneous Data Structure – Data elements may not be of same data type (ex: List, Tuples, Sets etc…).
What are Primitive and Non-Primitive data type?
Primitive and Non-Primitive Data Type
Primitive and Non-Primitive Data Types

Before understanding the functionality of the built-in data structure let us see a few built-in functions which will be used with data structure objects.

  • dir(obj) – a built-in function which will return the attribute and methods.
  • len(obj) – Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
  • del – This built-in keyword is used to delete an object from a namespace or remove items from an object like a list, dictionary, etc..
  • type(obj) – The type() function either returns the type of the object or returns a new type object based on the arguments passed.
  • id() – This function returns the “identity” of an object. This is an integer that is guaranteed to be unique and constant for this object during its lifetime.

Now as you have seen few important details, let us proceed on with python data structures.

Python comes with built-in data structures as well as users can define their own data structures. The built-in data structure includes LIST, DICTIONARY, TUPLE, and SET. Some of the examples for user-defined data structures are STACK, QUEUES, TREE, HASHMAP, etc…

People coming from other programming languages will be very familiar with an array type. But in python, they are not that common.

Here the list is kind of similar to an array but the list allows us to store values of any data type (Heterogeneous) while array will hold data of particular type only (int, float etc…). To use array you have to import the array from the “array” module explicitly.

In this Python series of articles, we will be looking at what is a data structure and python built-in data structure.

LIST

List is a data structure which is a collection of different data types. What does “collection of different data types” means? List can store strings, Integers, Floating point values, Nested list and so on.

List objects are “Mutable” which means items created inside the list can be Accessed, modified or deleted. List support indexing. Each item in the lists are assigned to an address and that address can be used to access or modify the particular item value.

  • Create a list
  • Insert/Access/Modify list
  • Delete list

CREATE LIST

List can be created using square brackets.

>>> name_empty = []			# Empty list
>>> name = ['Karthi', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']	# list with string data type
>>> name_int = [1,2,3]			# list with Integer data type
>>> name_mixed = [name_int,name,1,2,3.14]	# list with nested list items.
>>> name_mixed
[[1, 2, 3], ['Karthi', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will'], 1, 2, 3.14]
>>> name_int
[1, 2, 3]

We can use inbuilt type() function to check the object type.

>>> type(name)
Create List in Python
Create List in Python

We can access the methods and attributes of the list instance using dir() function.

>>> dir(name)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

We can find out the total number of items in the list using len() method.

>>> len(name)

We can create a new list from an existing list using list.copy() method.

>>> name_new = name.copy()
>>> name_new
['Karthi', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
Check Methods and Attributes of List
Check Methods and Attributes of List

INSERTING / ACCESSING / MODIFYING LIST

We can insert an item into a list at any position using list.insert(i, x) method.

>>> name = ['Leo','Matt','Kane','Scott','Petter','Will']
>>> name
['Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> name.insert(0,'Tom')	# Insert method takes 2 arguments (Index position, Item)
>>> name
['Tom', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']	# Tom is inserted at the 0th position.
Insert Items in List
Insert Items in List

We can use list.append(x) method to append a single item into the list. This will insert the item to the end of the list.

>>> name = []
>>> len(name)
0
>>> name.append('Leo')
>>> name.append('Matt')
>>> name.append('Kane')
>>> print(name)
['Leo', 'Matt', 'Kane']
Append Item in List
Append Item in List

We can use list.extend() method to add multiple items to the list.

>>> new_name = ['Gowtham','Martin','Luis']
>>> name.extend(new_name)
>>> name
['Will', 'Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will', 'Gowtham', 'Martin', 'Luis']
Add Multiple Items to List
Add Multiple Items to List

We can also use '+' operator to combine two list. Both the list can be of different data types.

>>> a = [1,2,3]
>>> b = [2,3,3]
>>> c = a + b
>>> c
[1, 2, 3, 2, 3, 3]
>>> d = ['karthi','kenny']
>>> e = a + d
>>> e
[1, 2, 3, 'karthi', 'kenny']
Combine Two List in Python
Combine Two List in Python

As already stated lists objects are mutable. A list item can be modified by referencing the index position and assigning value to it.

>>> name									# Before modified
['Tom', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> name[0] = 'Karthi'
>>> name									# After Modified
['Karthi', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
Modify List Item by Index Position
Modify List Item by Index Position

The list supports both positive and negative indexing.

Indexing starts from 0 and Negative Indexing starts from -1.

Python Indexing
Python Indexing

We can access the list item using their index position.

>>> name[0]			# Accessing the List item at index 0
'Leo'
>>> name[1]
'Matt'
>>> name[4]
'Petter'
>>> name[5]
'Will'
>>> name[-1]			# Accessing the list item with negative indexing
'Will'
>>> name[-6]
'Leo'
Access List Item Using Index Position
Access List Item Using Index Position

We can also use slicing to access the items in the list. Slicing allows us to access a range of items by defining the starting, ending, Step parameters.

# SYNTAX: list[starting position, ending position, Step]

>>> name[0:3]
['Tom', 'Leo', 'Matt']
>>> name[:]
['Tom', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> name[:4]
['Tom', 'Leo', 'Matt', 'Kane']
>>> name[:-2]
['Tom', 'Leo', 'Matt', 'Kane', 'Scott']
>>> name[:-1]
['Tom', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter']
>>> name[:-1:2]
['Tom', 'Matt', 'Scott']
Access Range of Items in List
Access Range of Items in List

We can find the number of occurence for a given value using list.count(x) method.

>>> name_int = [1,1,2,3,1]
>>> name_int.count(1)
3
Find Occurrence of Value
Find Occurrence of Value

We can find the Index position of a given item using list.index(x[, start[, end]]) method.

>>> name			# Inserted ‘Will’ at the end of the list. Now we have 2 name ‘Will’.
['Will', 'Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will']
>>> name.index('Will)	# Returns the index position of first occurence of x.
0
>>> name.index('Will',2)	# Starting index positon’2’ is given.
7
>>> name.index('Will',2,4)	# Starting and Ending Index position is given. Since there is no 					occurence of ‘Will’ within the given search position it will throw 					Value Error.
Traceback (most recent call last):
File "<stdin>", line 1, in 
ValueError: 'Will' is not in list

We can use list.reverse() method to reverse the items in the list.

>>> name
['Karthi', 'Leo', 'Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> name.reverse()
>>> name
['Will', 'Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi']
Reverse Items in List
Reverse Items in List

DELETE LIST

We can use list.pop(x) method to remove a item from a list at x position. This function will remove the item from the list and display the removed item. If x is not specified then pop() method will return the last item from the list.

>>> name
['Will', 'Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will', 'Gowtham', 'Martin', 'Luis']
>>> name.pop(0)
'Will'
>>> name
['Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will', 'Gowtham', 'Martin', 'Luis']
>>> name.pop()
'Luis'

We can also use list.remove (x) method to remove the item from the list. Here x takes the value of the item and throws an ValueError if x is not in the list.

>>> name = ['Leo','Matt','Kane','Scott','Petter','Will']
>>> name.remove('Leo')
>>> name
['Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> name.remove('Leo')
Traceback (most recent call last):
  File "", line 1, in 
ValueError: list.remove(x): x not in list

We can make a list empty by either assigning list name to square brackets or using list.clear() method.

>>> name1 = name.copy()
>>> name1
['Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will', 'Gowtham', 'Martin']
>>> name
['Petter', 'Scott', 'Kane', 'Matt', 'Leo', 'Karthi', 'Will', 'Gowtham', 'Martin']
>>> name = []			
>>> name
[]
>>> name1.clear()		
>>> name1
[]

Instead of using list methods to make the list empty or remove a item from the list we can use built in keyword del to perform this actions. The “del” keyword can delete a list object from memory or delete an item from a list or delete an item from a slice.

>>> name = ['Leo','Matt','Kane','Scott','Petter','Will']
>>> del name[0]
>>> name
['Matt', 'Kane', 'Scott', 'Petter', 'Will']
>>> del name[-3:]
>>> name
['Matt', 'Kane']
>>> del name[:]
>>> name
[]

Built in id() function return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime.

>>> id(name)
139979929658824
>>> del name
>>> id(name)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'name' is not defined

Note: we have removed the list variable from memory using del(), hence it throws name error.

help() funtion:

Built in help function() is very useful to get details about a particular object or methods of that object.

help(object)
help(object.method)
Summary

So far in this article, we have seen how we can use a list data structure to store, access, modify, delete list objects by using the list methods. We have also seen some built-in functions like id(), dir(), type(), help() which are very effective functions. We also have list comprehension in python which provides a more concise and readable way of creating a list.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Karthick Sudhakar
I am a Big data Engineer from India, Chennai working on a top MNC with a vision of being a bridge between technology and mankind.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

3 thoughts on “Learn Python List Data Structure – Part 1”

  1. Thanks for this great info.

    I have points that you might improve.

    First about copy() method of the list. It is better to mention notions of a shallow and deep copy. It was hard for me a few years ago to understand why my copied object still modified even if I created a copy of it and did modifications over a copy.

    And the second image that represents list indexing missing the first element pls update it.

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.