Python lists can store a collection of data. We can say its similar to arrays in other programming languages. Unlike arrays, a list in python can also store items of different data types. For instance, a single list can be a collection of strings, integers, tuples, etc. In this tutorial, we are going to discuss more on Python lists in detail.
Python Lists can be defined by placing items inside the square brackets. A list can contain duplicate elements, which means, the elements can appear more than once.
l=[1,’abc’,(1,2),1]
This is how we define a list in Python. As mentioned previously, here we are saving elements of different data types in a single list. Element (1,2) is of datatype tuple, ‘abc’ is of datatype string and 1 is of datatype string. We will be learning about tuples in the next tutorial.
An empty list can be defined by empty square brackets
l=[]
Table of Contents
Accessing list elements
Python Lists elements can be accessed similar to strings, using their
- Positive index
- Negative index
- Range of positive index and negative index
l=[1,2,3,'a','b','c'] #Postive index print(l[1]) #Negative index print(l[-4]) #Range of positive index print(l[0:4]) #Range of negative index print(l[-5:-1])
However, the most common way to access the elements of the list is through for loop.
l=[1,2,3,'a','b','c'] for i in l: print(i)
Changing Python List elements
Python lists are mutable, meaning we can add/update/delete the items in a list.
Update in Python Lists
To change the value of a specific item, refer to it’s index number
l=[1,2,3,'a','b','c'] l[-1]='d' print(l)
Addition in Python Lists
append()
append() add the element at the end of the list. append() takes exactly one argument.
extend()
extend() also adds elements to the list. But extend() adds elements from one list to the end of another list. If you add one list to another using append(), the new list will be added as a new nested list to the existing list.
‘+’ operator
Another way to concatenate two lists is by using ‘+’ operator
insert()
In case, you want to add an element at a specific index, then insert() is the one to use.
Examples:
# insert l=[1, 2, 3, 'a', 'b', 'd'] l.insert(5,'c') print(l) # append l=[1,2] l.append('a') l.append('c') print(l) #extend l=[1,2] l.extend(['a','b']) print(l) #Difference between append and extend l=[1,2] l.append(['a','b']) print(l) # '+' operator print([1,2]+['a','b'])
Deletion in Python Lists
pop()
pop() removes the element at the specified index. It deletes the last element if the index is not mentioned.
del
del keyword removes the element at the specified index. If the index is not mentioned, the entire list will be deleted.
clear()
clear() method empties the list
Examples
# pop l=[1, 2, 3, 'a', 'b', 'c'] l.pop(1) print(l) l.pop() print(l) # del l=[1, 3, 'a', 'b', 'c'] del l[-1] print(l) del l print(l) # clear l=[1, 3, 'a', 'b', 'c'] l.clear() print(l)
Deep and shallow copy
Deep copy means that any changes made to a copy of the object do not reflect in the original object
Shallow copy means that any changes made to a copy of the object do reflect in the original object
Below code explains how we can do deep and shallow copy in Python Lists
# Shallow copy l=[1,2,3] l1=l l1.pop() print(l1,l) # Deep Copy l=[1,2,3] l1=l.copy() l1.append('a') print(l1,l)
Other very basic list operations:
len()
len() is to find the length of the list
list()
list() constructor is to make a List
in operator
To check if the specified item is present in a list.
Examples:
# len() print(len([1,2,3,'a','b','c'])) # list() print(list('abc')) print(list('bc',1)) # in l=[1,2,3,'a','b','c'] print('a' in l) print('d' in l)
Conclusion
Through loops, there are many ways to access the list elements. We will be covering those in the loops tutorial. You can more methods we can use on lists, in the documentation page.