List Functions

List Functions

Python has a lot of list methods that allow us to work with lists. In this tutorial, you will find all the list methods to work with Python lists.

Python List Functions
1. len()

Returns the number of elements present in the list.

E.g

n = [10, 20, 30, 40] 
print(len(n) ==> 4
2. count()

It returns the number of occurrences of specified item in the list.

n=[1,2,2,2,2,3,3] 
print(n.count(1)) // 1
print(n.count(2)) // 4
print(n.count(3)) // 2
print(n.count(4)) // 0
3. index()

Returns the index of first occurrence of the specified item.

n = [1, 2, 2, 2, 2, 3, 3] 
print(n.index(1)) // 0 
print(n.index(2)) // 1 
print(n.index(3)) // 5 
print(n.index(4)) // ValueError: 4 is not in list

Note

If the specified element not present in the list then we will get ValueError. Hence before index() method we have to check whether item present in the list or not by using in operator.

print( 4 in n) // False
4. append()

We can use append() function to add item at the end of the list.

list=[] 
list.append("A") 
list.append("B") 
list.append("C") 
print(list)

Output

> py test.py
['A', 'B', 'C']

E.g: To add all elements to list up to 100 which are divisible by 10.

list=[] 
for i in range(101): 
    if i%10==0: 
        list.append(i) 
print(list)

Output

> py test.py
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
5. insert()

To insert item at specified index position

n=[1,2,3,4,5] 
n.insert(1,888) 
print(n)

Output

> py test.py
[1, 888, 2, 3, 4, 5]

E.g 2

n=[1,2,3,4,5] 
n.insert(10,777) 
n.insert(-10,999) 
print(n)

Output

> py test.py
[999, 1, 2, 3, 4, 5, 777]

Note

If the specified index is greater than max index then element will be inserted at last position. If the specified index is smaller than min index then element will be inserted at first position.

6. pop()

The pop() method removes the item at the given index from the list and returns the removed item. The syntax of the pop() method is

list.pop(index)
  • The pop() method takes a single argument (index).
  • The argument passed to the method is optional. If not passed, the default index -1 is passed as an argument (index of the last item).
  • If the index passed to the method is not in range, it throws IndexError: pop index out of range exception.
  • The pop() method returns the item present at the given index. This item is also removed from the list.
languages = ['Python', 'Java', 'C++', 'Go', 'C']

returnValue = languages.pop(3)
print('Return Value:', returnValue)

print('Updated List:', languages)

Output

Return Value: Go
Updated List: ['Python', 'Java', 'C++', 'C']

E.g 2

languages = ['Python', 'Java', 'C++', 'Go', 'C']

print('When index is not passed:') 
print('Return Value:', languages.pop())
print('Updated List:', languages)

print('\nWhen -1 is passed:') 
print('Return Value:', languages.pop(-1))
print('Updated List:', languages)

print('\nWhen -3 is passed:') 
print('Return Value:', languages.pop(-3))
print('Updated List:', languages)

Output

When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Go']

When -1 is passed:
Return Value: Go
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']

If the list is empty then pop() function raises IndexError

n = []
print(n.pop()) ==> IndexError: pop from empty list
7. count()

The count() method returns the number of times the specified element appears in the list. The syntax of the count() method is:

list.count(element)
  • The count() method takes a single argument.
  • The count() method returns the number of times element appears in the list.
vowels = ['a', 'e', 'i', 'o', 'i', 'u']

count = vowels.count('i')
print('The count of i is:', count)

count = vowels.count('p')
print('The count of p is:', count)

count = vowels.count('u')
print('The count of u is:', count)

Output

The count of i is: 2
The count of p is: 0
The count of p is: 1

E.g 2

random = ['a', ('a', 'b'), ('a', 'b'), [3, 4]]

count = random.count(('a', 'b'))
print("The count of ('a', 'b') is:", count)

count = random.count([3, 4])
print("The count of [3, 4] is:", count)

Output

The count of ('a', 'b') is: 2
The count of [3, 4] is: 1
8. reverse()

The reverse() method reverses the elements of the list. The syntax of the reverse() method is

list.reverse()
  • The reverse() method doesn’t take any arguments.
  • The reverse() method doesn’t return any value. It updates the existing list.
systems = ['Windows', 'macOS', 'Linux']
print('Original List:', systems)

systems.reverse()
print('Updated List:', systems)

Output

Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

If you need to access individual elements of a list in the reverse order, it’s better to use reversed() function.

systems = ['Windows', 'macOS', 'Linux']

for o in reversed(systems):
    print(o)

Output

Linux
macOS
Windows
9. sort()

The sort() method sorts the elements of a given list in a specific ascending or descending order. The syntax of the sort() method is

list.sort(key=..., reverse=...)

Alternatively, you can also use Python’s built-in sorted() function for the same purpose.

sorted(list, key=..., reverse=...)

The simplest difference between sort() and sorted() is: sort() changes the list directly and doesn’t return any value, while sorted() doesn’t change the list and returns the sorted list.

By default, sort() doesn’t require any extra parameters. However, it has two optional parameters:

  • reverse – If True, the sorted list is reversed (or sorted in Descending order)
  • key – function that serves as a key for the sort comparison

The sort() method doesn’t return any value. Rather, it changes the original list.

If you want a function to return the sorted list rather than change the original list, use sorted().

vowels = ['e', 'a', 'u', 'o', 'i']

vowels.sort()

print('Sorted list:', vowels)

Output

Sorted list: ['a', 'e', 'i', 'o', 'u']

The sort() method accepts a reverse parameter as an optional argument.

Setting reverse = True sorts the list in the descending order.

list.sort(reverse=True)

Alternately for sorted(), you can use the following code.

sorted(list, reverse=True)

E.g

vowels = ['e', 'a', 'u', 'o', 'i']

vowels.sort(reverse=True)

print('Sorted list (in Descending):', vowels)

Output

Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']
10. clear()

The clear() method removes all items from the list. The syntax of clear() method is

list.clear()
  • The clear() method doesn’t take any parameters.
  • The clear() method only empties the given list. It doesn’t return any value.
list = [{1, 2}, ('a'), ['1.1', '2.2']]

list.clear()

print('List:', list)

Output

List: []
List Functions
Scroll to top