Back to Blog Home

Python Lists and Common Python List Operations, Methods and Functions

Ishaan Bhola| Python | 6 months, 3 weeks



 

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[0])    # 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

 

List Operations Python :

Slicing of Python Lists

 

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 )

 

Output :

[2,3,7]

 

Now,

print(list[:2])

 

Output:

[2,3]

 

prints everything from 0 index number upto index 2 ( but not including 2 ).

Similarly,

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.

 

Output :

[3,7,12]

 

Updating Python Lists

Lists in python are mutable and values of items can be changed anytime. 

 

For example, 

list = [1, 4, 100, 9 ]
list[3] = ‘Enpointer’     #changes value of index item 3 
print(list)

 

Output :

list = [1, 4, 100, 'Enpointer']

 

Python List Methods 

 

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. 

 

1. list.append(element)

 

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)

 

Output :

[2,5,4,10,1]

 

2. list.insert(index, element)

 

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)

 

Output :

[2,9,5,4,10,1]

 

3. list.remove(element)

 

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)

 

Output :

[2,5,10]

 

4. list.pop(index)

 

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)

 

Output :

10
[2,5,4]

 

5. list.sort()

 

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)

 

Output :

[2,4,5,10]

 

If you have to sort the list from high to low, just put reverse = True as argument, for eg :

list.sort(reverse=True)

 

Similarly , you can use the list sort method to sort string data type lists alphabetically from a-z or z-a (using reverse=True)

 

6. list.count(element)

 

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))

 

Output :

2

 

7. list.extend(another_list)

 

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)

 

Output :

[4,4,8,2,1]

 

This output can be achieved by using ‘+’ operator between lists as well

list1 = [4, 4, 8]
list2 = [2,1]
print(list1+list2)

 

Output :

[4,4,8,2,1]

 

Python List Operations

 

Python lists support all the major operations that you can do with Python strings. Lets take a look.

 

1. Concatenation

 

You can add two lists using + operator 

[1,3,4] + [3,7,9]

 

Output 

[1,3,4,3,7,9]

 

2. Membership 

 

You can test if an element is member of list or not. Returns a boolean

4 in [1,3,4,3,7,9]

 

Output:

True

 

3. Iteration

 

You can iterate over the lists as well.


for x in [3,5,6]
print x

 

Output:

3 5 6

 

4. Repetition

 

You can use * operator to repeat elements in a list. 

3*['Enpointer']

 

Output:

['Enpointer','Enpointer','Enpointer','Enpointer']

 

Python List Functions

 

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:

 

1. len(list)

Finds the number of elements in the list.

 

2. max(list) or min(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. 

 



Join 1000+ People Who Subscribe to Weekly Blog Updates

Back to Blog Home