Python list object and its methods with examples

Python list is a container, a doubly-linked list that stores any kind of values. It has a whole set of supported methods that are useful to do any sort of operation on this. All functions are as follows.

  1. append
  2. count
  3. copy
  4. insert
  5. extend
  6. pop
  7. remove
  8. clear
  9. index
  10. reverse
  11. sort

append

The append method adds the input parameter to the list object. Even it combines another iterable into the existing list object. This requires only one parameter and returns None.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
print(list1)
#output prints
[5, 10, 15, 20, 25, 30, 12]

count

Count method gets the number of occurrences of the parameter value in the list. This method, too, requires only one parameter and returns the number of instances.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
print(list1.count(5))
#output prints
1

copy

Copy method copies all the elements in the list into another list. It requires no parameters and returns another copied list.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
print(list2)
#output prints copied list.
[5, 10, 15, 20, 25, 30, 12]

insert

Insert method inserts a value in the given index. It requires an index parameter, a value parameter, and returns None.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)
print(list2)
#output prints
[5, 10, 15, 9, 20, 25, 30, 12]

extend

This method extends the list with iterable (container) parameter values. It requires a single iterable parameter and returns None.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)
list2.extend([1, 2])
print(list2)
#output prints
[5, 10, 15, 9, 20, 25, 30, 12, 1, 2]

pop

This method removes the last item from the list and returns it. This function requires no parameters, whereas return the previous object.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)
list2.extend([1, 2])
val = list2.pop()
print(val)
#output prints
2

remove

This method removes the object in the parameter form the list. This function requires a parameter and removes it from the list and returns None.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)
list2.extend([1, 2])
val = list2.pop()
list2.remove(5)
print(list2)
#output prints
[10, 15, 9, 20, 25, 30, 12, 1]

clear

This method clears all the list elements and makes a list is empty. This method requires no parameters and returns none.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)e
list2.extend([1, 2])
print(list2)
val = list2.pop()
print(val)
list2.remove(5)
print(list2)
list2.clear()
print(list2)
#output prints
[]

index

The index method gives an index of the value in the list. It requires an object of which we want to find the index and returns the index of that value.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list2 = list1.copy()
list2.insert(3, 9)
list2.extend([1, 2])
val = list2.pop()
list2.remove(5)
list2.clear()
print(list1.index(20))
#output prints
3

reverse

This method reverses all objects in the list. It requires no parameters and returns none.

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list1.reverse()
print(list1)
#output prints
[12, 30, 25, 20, 15, 10, 5]

sort

Finally, the sort method sorts all the objects in the list in ascending order. It also requires no parameters and returns none, whereas it has some optional parameters, which are callable and a reverse order (like ascending or descending).

list1 = [5, 10, 15, 20, 25, 30]
list1.append(12)
list1.reverse()
list1.sort()
print(list1)
#output prints
[5, 10, 12, 15, 20, 25, 30]

These are all different methods supported in python list object. Here in this article, we are not mentioning about optional parameters and their use cases. If you know any other parameter usage of these methods, please specify in the below comments and help our readers to understand these methods better. Happy Learning!.

Advertisements