11 Python list methods

By: On:

In this python tutorial, you will learn about the different built-in list methods in Python which is used to modify the list. Here, we will see various python list methods that can be used in the Python list. Here we will check below Python list methods.

  • Python list methods
  • Python List append() method
  • Python List extend() method
  • Python List insert() method
  • Python List remove() method
  • Python List count() method
  • Python List clear() method
  • Python List copy() method
  • Python List index() method
  • Python List reverse() method
  • Python List pop() method
  • Python List sort() method
  • Python list len() method

Python list methods

Here, we will see various python list methods that are very much useful to work with list in Python.


Python List append() method

In python, the append() method adds a single element to the end of the existing list.

Example:

vegetable = ['Corn', 'Tomato', 'Broccoli']
vegetable.append('Cucumber')
print(vegetable)

After writing the above code (python list append method), Ones you will print “vegetable” then the output will appear as a “[ ‘Corn’, ‘Tomato’, ‘Broccoli’, ‘Cucumber’ ]”. Here, I have append the Cucumber and it is added to the list in the last. You can refer to the below screenshot for creating a python list append().

Python List append() method
Python List append() method

If we want to add more than, one element in the list then it will make a list of lists, we can see the below screenshot.


append method in python list

Python List extend() method

The extend() method in python 3 add all the elements of an iterable to the end of the list. The extend methods add all the values in one list.

Example:

color = ['Red', 'Pink', 'Orange']
color2 = ['Blue', 'Yellow']
color.extend(color2)
print(color)

After writing the above code (python list extend), Ones you will print “color” then the output will appear as a “[ ‘Red’, ‘Pink’, ‘Orange’, ‘Blue’, ‘Yellow]”. Here, I have extend color2 and it has extended the list. You can refer to the below screenshot for creating a python list append().

Python List extend() method
Python List extend() method

Python List insert() method

The Python list insert() method is used to insert the element in the list at the given index. As we know the element in the list starts with index 0.

Example:

name = ['Rita', 'Pinky', 'Babita']
name.insert(1, 'Mini')
print(name)

After writing the above code (python list insert), Ones you will print “name” then the output will appear as a “[ ‘Rita’, ‘Mini’, ‘Pinky’, ‘Babita’]”. Here, I have given the index 1 to insert the element. You can refer to the below screenshot for creating a python list insert() method.

python list insert method
Python List insert() methods

Python List remove() method

The remove() method in Python removes the matching element from the list. It will search for the given element in the list, If the given element matches then it is removed.

Example:

roll = [1,2,3,4,5]
roll.remove(3)
print(roll)

After writing the above code (python list remove), Ones you will print “roll” then the output will appear as a “[ 1,2,4,5 ]”. Here, 3 have been removed from the list. You can refer to the below screenshot for creating a python list remove() method.

Python List remove() method
Python List remove() method

Python List count() method

The Python list count() method is used to count the number of particular element present in the list.

Example:

place = ['Delhi', 'Bangalore', 'kolkata', 'Delhi']
value = place.count('Delhi')
print(value)

After writing the above code (python list count), Ones you will print “value” then the output will appear as a “ 2 ”. Here, 2 times Delhi is present in the list. You can refer to the below screenshot for creating a python list count() method.

python list count method
Python List count() method

Python List clear() method

The python list clear() method clear all the elements present in the list. The clear() method doesn’t take any parameters.

Example:

place = ['Delhi', 'Bangalore', 'kolkata']
place.clear()
print(place)

After writing the above code (python list clear), Ones you will print “place” then the output will appear as a “[ ] ”. Here, it clear the list and it returns no value. You can refer to the below screenshot for creating a python list count() method.

python list clear method
Python List clear() method

Python List copy() method

The copy() methods in Python list return the new list, which is exactly the same. The copy() method doesn’t take any parameters.

Example:

chocolate = ['Kitkat', 'Bournville', 'Snickers']
chocolate.copy()
print(chocolate)

After writing the above code (python list clear), Ones you will print “chocolate” then the output will appear as a “[ ‘Kitkat’, ‘Bournville’, ‘Snickers’ ] ”. Here, copy methods copy the list and it returns the same element present in the list. You can refer to the below screenshot for creating a python list count() method.

python list copy method
Python List copy() method

Python List index() method

The Python list index() method returns the index of the specified elements from the list. The index method takes parameters.

Example:

flower = ['Flora', 'Hana', 'Rose']
name = flower.index('Hana')
print(name)

After writing the above code (python list clear), Ones you will print “name” then the output will appear as a “ 1 ”. Here, it gives the index of the specified value from the list. You can refer to the below screenshot for creating a python list count() method.

list index in python
Python List index() method

Python List reverse() method

The Python list reverse() method is used to reverse the element present in the list.

Example:

emp_id =[12,13,14,15]
emp_id.reverse()
print(emp_id)

After writing the above code (python list reverse), Ones you will print “emp_id” then the output will appear as a “[15,14,13,12 ] ”. Here, it will reverse the whole list element. You can refer to the below screenshot for creating a python list count() method.

list reverse in python
Python List reverse() method

Python List pop() method

The pop() method in Python list is used to remove the specified item from the given index of a list.

Example:

emp_id = [12,13,14,15]
emp_id.pop(1)
print(emp_id)

After writing the above code (python list clear), Ones you will print “emp_id” then the output will appear as a “[ 12,14,15 ] ”. Here, it will pop the value of index 1 which is 13. You can refer to the below screenshot for creating a python list count() method.

python list pop method
Python List pop() method

Python List sort() method

The Python list sort() method is used to sort the list in ascending order by default it, doesn’t require an extra parameter to sort.

Example:

bike = ['Splendor', 'Royal Enfield', 'Pulsar']
bike.sort()
print(bike)

After writing the above code (python list clear), Ones you will print “bike” then the output will appear as a “[ ‘Pulsar’, ‘Royal Enfield’ , ‘Splendor’ ] ”. Here, it sort the list in ascending order. You can refer to the below screenshot for creating a python list count() method

python list sort
Python List sort() method

You can also use the built-in sort() method to sort() it in descending order. By giving the parameters “reverse=True”.

Example:

bike = ['Splendor', 'Royal Enfield', 'Pulsar']
bike.sort(reverse=True)
print(bike)

You can refer to the below screenshot for creating a python list sort() method for descending order.

Python List sort() method

The sort() method is also used to sort the list by the length of the value present in the list.

Example:

def Function(e):
return len(e)
bike = ['Pulsar', 'Royal Enfield', 'Splendor']
bike.sort(key=Function)
print(bike)

You can refer to the below screenshot for creating a python list sort() method to arrange the element according to the length of the value.

sort method in python list

Python List len() method

In python, the built-in len() method is used for getting the total number of elements present in the list. In the list, the items can be the combination of numbers and string.

Example:

my_list = [20, 34, 39, 'Apple', 'Mango', 'Orange']
print(" The length is: ", len(my_list))

After writing the above code (Python List len() method), Ones you will print “ len(my_list)” then the output will appear as a “ The length is: 6”. Here, the len() method will give the length of the item present in the list. You can refer to the below screenshot for the python List len() method.

Python List len() method

These are various python list methods and list methods python 3.


You may like following Python tutorials:

In this Python tutorial, we learned about various list methods in python like:

  • Python list methods
  • Python List append() method
  • Python List extend() method
  • Python List insert() method
  • Python List remove() method
  • Python List count() method
  • Python List clear() method
  • Python List copy() method
  • Python List index() method
  • Python List reverse() method
  • Python List pop() method
  • Python List sort() method
  • Python list len() method

Leave a Comment