93 / 100

Python List Methods With Example

Today in this tutorial, we will learn the python list methods implementation with example. At the end of the tutorial, you will learn about list methods in python programming language and the core idea of the python list methods.

python list methods

In Python programming language, Python list methods are mostly used for listing because the list accepts all the data types of items and in an ordered way. In Python, List can be implemented in two ways:

  • list() :- It is a list function. for eg.: list(vowel)
  • [ ] :- It is used as a list. for eg.: name_list = [‘Tom’, ‘Bob’, ‘Jack’]

Example of the list in python

python list

Code

#list in python
name_list = ['Tom', 'Jack', 'Ram', 'Sita']
print(name_list)

Python List Methods

Python list methods are many type like

  • append()
  • clear()
  • copy()
  • count()
  • extend()
  • index()
  • insert()
  • pop()
  • remove()
  • reverse()
  • sort().

Python list append()

Python list append() method is used for appending the item in the list. It appends the single item at the last of the list item.

The syntax of the append() is : list.append(item)

Example

append method in python

Code

#list in python
name_list = ['Tom', 'Jack', 'Ram', 'Sita']

#to append the item
name_list.append('mira')

#print the list item
print(name_list)

In the above code, adding an item ‘mera’ in the name_list. See in the output the item ‘mira’ is listed in the name_list at the last item.


Python list clear()

Python list copy() method is used to clear the list item. By using clear() function, all the item can be removed from the list at a time.

The syntax of the clear() is : list.clear(item)

Example

python clesr list method

Code

#list
name_list = ['Tom', 'Jack', 'Ram', 'Sita']

#print before the clear list
print('Before clear list :', name_list)

#clear list
name_list.clear()

#print after the clear list
print('After clear list :',name_list)

As you can see in the above code, clear() method is used to clear the list item. In the output, 1st line is printing the list item before using clear() method but in the 2nd line of output printing the list item after using clear() method. So, in the second line of the output list is empty.


Python list copy()

Python list copy() method is used to copy the list item to another list at a time.

The syntax of the copy() is : new_list =  list.copy()

Example

copy list method

Code

#list
name_list = ['Tom', 'Jack', 'Ram', 'Sita']

#copy() to new list
new_list = name_list.copy()

#print the new list item
print(new_list)

In the above code, as you can see the name_list is the old list and the new_list is the new one. By using copy(), the list item copy into New_list.


Python list count()

Python list count() method is used to count the occurrence of number for selected item present in the list.

The syntax of the count() is : list.count(item)

Example

count list method

Code

#list
letter = ['a', 'd', 'k', 'i', 'a', 'i', 'a']

#count the element
count = letter.count('a')

#print the counted number
print(count)

In the above program, the count() method is used to count the occurrence of the item. As you can see the letter a occurs 3 times.


Python list extend()

Python list extend() method is used to extend the list by adding all the iterable element at the end of the list like list, string, tuples.

The syntax of the extend() is : list1.extend(list2)

Example

python list extend method

Code

#two list
list1 = ['apple', 'banana']
list2 = ['grapes', 'papaya']

#extend the list1
list1.extend(list2)

#print the list by extending in list1
print(list1)

In the above program, the extend() method is used to extend the list1 by adding list2 as you can see the the output


Python list index()

Python list index() method is used to find the index of the element. It only index the fist occurred element only. It will take a maximum of 3 arguments. If the element is not found in the list then ValueError exception is raised.

  • element – the element to be searched
  • start (optional) – start searching from this index
  • end (optional) – search the element up to this index
The syntax of the index() is : list.index(element, start, end)

Example

Python list index method

Code

#list
alphabets = ['a', 'd', 'e', 'f', 'a', 'z', 'e']

#finding the index of element 'a'
index = alphabets.index('a')
print(index)

#find the index of a & start searching from index 1
index = alphabets.index('a', 1)
print(index)

#find index of a, which search between index 0 to 3
index = alphabets.index('a', 0, 3)
print(index)

In the above code, the index of the element ‘a’ is searched in many ways. In the first line of the output, give the output for just finding the index of element ‘a’, in 2nd line of output, gives the index of ‘a’ which start from searching element from index 1 and in 3rd line of output, gives the index of ‘a’ which start searching the element between index 0 to 3.


Python list insert()

Python list insert() method is used to insert the element in the list. Insert() method used 2 parameters index and element. When inserting the element, all the elements are shifted to the right.

  • index – the index where the element needs to be inserted
  • element – this is the element to be inserted in the list
The syntax of the insert() is : list.insert(index, element)

Example

python list insert method

Code

#list
name_list = ['Tom', 'Jack', 'Ram']

#insert Bob at index 1
name_list.insert(1, 'Bob')

#print name_list
print(name_list)

in the above program, the insert() method is used to insert the element at a fixed index. As you can see, in the name list the element is inserting at index 1, and all the elements from index 1 are shifted toward the right.


Python list pop()

Python list pop() method is used to remove the element at the given index from the list. pop() method use 1 argument i.e. index, which is optional. By default the pop() function uses -1 index, which is the last element of the list, and if the given index is out of range then it throws IndexError: pop index out of range exception.

The syntax of the pop() is : list.pop(index)

Example

python list pop method

Code

#list
name_list = ['Tom', 'Bob', 'Jack', 'Ram']

#use pop() method to remove the element
name_list.pop(1)

#print the name_list
print(name_list)

In the above code, pop() method is used to remove the element of the given index. As you can see at index 1 element is removed from the list.


Python list remove()

Python list remove() method is used to remove the first matching element from the list. remove() method is to use single arguments. If the element is not present in the list, then it throws ValueError: list.remove(x): x not in list exception.

The syntax of the remove() is : list.remove(item)

Example

python list remove method

Code

#list
name_list = ['Tom', 'Bob', 'Jack', 'Bob']

# remove the element from the list
name_list.remove('Bob')

#print the name_list
print(name_list)

In the above code, remove() method is used to remove the element from the list. As you can see, the element ‘Bob’ is removed which comes first in the list.


Python list reverse()

Python list reverse() method is used to reverse the list.

The syntax of the reverse() is : list.reverse()

Example

Python list reverse method

Code

#list
name_list = ['Tom', 'Bob', 'Jack']

#reverse the list
name_list.reverse()

#print the list
print(name_list)

In the above program, the reverse() method is used to reverse the list. As you can see, the list is reverse.


Python list sort()

Python list sort() method is used to sort the given list in a specific ascending or descending order.

The syntax of the sort() is : list.sort()

Example

Python list sort method

Code

#list
name_list = ['Tom', 'Bob', 'Jack']

# sort the list
name_list.sort()

#print list
print(name_list)

In the above program, the list is sorted in the alphabetical ascending order.

Video tutorial of Python list methods


Learn more python programming language

FAQ for Python List Methods

What is python list append method?

In Python list methods, the append() method is used to add the element to the list at the last position. The syntax of the append() method is : list.append(item).

What is python list clear method?

In Python list methods, the clear() method is used to remove all the elements of the list. The syntax of the clear() method is : list.clear(item).

What is python list copy method?

In Python list methods, the copy() method is used to copy the list elements to another list. The syntax of the copy() method is : new_list = list.copy().

What is python list count method?

In Python list methods, the count() method is used to count the iteration of given item. The syntax of the count() method is : list.count(item)

What is python list extend method?

In Python list methods, the extend() method is used to extend the list item by add the items of another list. The syntax of the extend() method is : list1.extend(list2).

What is python list index method?

In Python list methods, the index() method is used to find the index of the given element. The syntax of the index() method is : list.index(element, start, end).

What is python list insert method?

In Python list methods, the insert() method is used to insert the element at the given index to the list. The syntax of the insert() method is : list.insert(index, element)

What is python list pop method?

In Python list methods, the pop() method is used to remove the given index from the list. The syntax of the pop() method is : list.pop(index)

What is python list remove method?

In Python list methods, the remove() method is used to remove the given element. The syntax of the remove() method is : list.remove(item)

What is python list reverse method?

In Python list methods, the reverse() method is used to reverse the list. The syntax of the reverse() method is : list.reverse()

What is python list sort method?

In Python list methods, the sort() method is used to sort the list in a specific way. The syntax of the sort() method is : list.sort()

3 thoughts on “Python List Methods With Example”

Leave a Comment

Share via
Copy link
Powered by Social Snap