A list in python is a collection of items and those items can be of heterogeneous nature or of different data types. A list in python is represented with square brackets, for example :
list = [ 4, 23, 19 ]
This is an example of a homogeneous python list. It is homogeneous because all the items in the list are of same data type i.e. int
list = [4, 23, 19, ‘world’ , ‘Enpointer’ , True, 3.14 ]
In this list the items are of different data types i.e string, int, bool and float, so it is a heterogeneous python list.
A list is essentially a sequence and all items are ordered starting from an index number of 0 for the first item. These indices or index numbers can be used for accessing items in the list, for example :
list = [ 2, 6, 8, ‘new’ ] print(list) # accessing the 0 index item in the list ‘list’ 2 #gives first item value of the list
Now lets suppose you have to get the last item of the list and somehow you don’t know the length of the list. For such kind of use cases python supports negative indexing for the lists.
print(list[-1]) #print first item from the last of the list new # first item from last of the list is printed
You can slice a list across a particular index number or across a range.
list = [2, 3, 7, 12] print(list[0:3]) #slice a list from index 0 upto index 3 (but not including index 3 )
prints everything from 0 index number upto index 2 ( but not including 2 ).
list = [2, 3, 7, 12] print(list[1:])
will print all items from index 1 to end of list. Important point to be noted here is that in this code index item 1 is included. So index number defined on left in slicing are included while the ones on the right are not included.
Lists in python are mutable and values of items can be changed anytime.
list = [1, 4, 100, 9 ] list = ‘Enpointer’ #changes value of index item 3 print(list)
list = [1, 4, 100, 'Enpointer']
List methods are used to modify the original list. It is important to note that methods which are performed on the list modify the original list itself.
Appends a new element at the end of the list. It modifies the original list and returns None.
list = [2, 5, 4, 10] list.append(1) print(list)
This method adds the element at the given index and shifts the list to the right.
list = [2, 5, 4, 10] list.insert(1,9) print(list)
Deletes the element where it occurs first time on the list and if the element is not available in the list, it throws an error.
list = [2, 5, 4, 10] list.remove(4) print(list)
This python list method removes the element from the given index and returns the element. If index is not provided then it removes the element on last index.
list = [2, 5, 4, 10] print(list.pop(3)) print(list)
This python list method sorts the original list. Another common mistake people do with this method is that they assume it returns the sorted list while it doesn’t return it.
Default order it follows is low to high.
list = [2, 5, 4, 10] list.sort() print(list)
If you have to sort the list from high to low, just put reverse = True as argument, for eg :
Similarly , you can use the list sort method to sort string data type lists alphabetically from a-z or z-a (using reverse=True)
This python list method counts number of times a value occurs in a list.
list = [2, 3, 7, 4, 14, 4] print(list.count(4))
This python list method takes the elements of ‘another_list’ and adds it at the end of list.
list = [4, 4, 8] list.extend([2,1]) print(list)
This output can be achieved by using ‘+’ operator between lists as well
list1 = [4, 4, 8] list2 = [2,1] print(list1+list2)
Python lists support all the major operations that you can do with Python strings. Lets take a look.
You can add two lists using + operator
[1,3,4] + [3,7,9]
You can test if an element is member of list or not. Returns a boolean
4 in [1,3,4,3,7,9]
You can iterate over the lists as well.
for x in [3,5,6] print x
3 5 6
You can use * operator to repeat elements in a list.
There are some built in functions as well for python lists. Also do note the difference between methods and functions.
Here are some common ones:
Finds the number of elements in the list.
Returns maximum or minimum element value from the list.
So I hope you have grasped the concept of Python lists and it’s manipulation through various list methods, list functions and list operations.