Working with list methods in python with examples

In python programming language list is something different from the array. We have explained various list methods in python in this guide with examples.

An array only contains the same data type elements while a list can contain more than one data type element.

So python provides so many methods of the list by which we can perform some operations on the list.

Now let’s talk about some list methods in python.

1. append() method

The append() method in python is very useful. And this method is used to add an element at the end of the list.

Syntax : 

list.append(element)

The append() method takes an element to be added as an input parameter. The elements can be a string, an integer, or an object.

Example 1:

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’
list_1.append(‘gorilla_city’)
print(‘updated list : ’, list_1)

Output :

updated list : [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’, ‘gorilla_city’]

Example 2:

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
list_2 = [‘argo_city’, ‘coast_list’]
list_1.append(list_2)

print(‘updated list : ’, list_1)

Output :

 updated list :  [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’, [‘argo_city’, ‘coast_list’] ]

2. clear() method :

The clear() method is used to clear a list. This method does not take any input argument and just removes all the elements from the list.

Syntax :

list.clear()

Example code :

    list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
    list_1.clear()
print(‘the updated list :’, list_1)

Output :

The updated list : [ ]

3. copy() method :

The copy() method is used to copy the elements from one list into another list. There are several methods for copying the elements of a list into another list.

Syntax :

another_list = list.copy()

Example code:

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
list_ 2 = list_1

# if we want to copy a list then we will face a problem, when we will modify the list_2 then the list_1 will automatically be modified.
list_2.append(‘gorilla_city’)

print(‘list_1’, list_1)
print(‘list_2’, list_2)

Output : 

list_1 : [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’,’gorilla_city’]

list_2 : [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’, ‘gorilla_city’]

If we want the elements of the original list will remain unchanged then we have to use a copy() method.

Example 2:

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
list_2 = list_1.copy()

print(‘list_2 : ’, list_2)

Output :

list_2 : [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]

4. count() method

The count() method is used to count the elements in the list. This method takes input as an element to be count.

Syntax :

i = list.count(element)

Example code :

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’, ‘gotham_city’,]
i = list_1.count(‘gotham_city’)

print(‘the no of times gotham_city appears in the list is : ’, i)

Output:

the no of times gotham_city appears in the list is: 2

5. sort() method

The sort() function is basically used to sort the elements of a list. This function requires two optional parameters.

Syntax :

list.sort()

Example code 1:

list_1 = [9,8,7,6,5,4,3,2,1]
This mehthod list_1.sort()

print(‘sorted list : ‘, list_1 )

Output:

sorted list : 1,2,3,4,5,6,7,8,9

Example code 2:

If you want to sort a list in descending order then you should go like this.

list_1 = [1,2,3,4,5,6,7,8,9]
list_1.sort(reverse = true)

print(‘sorted list in descending order : ’, list_1)

Output: 

Sorted list in descending order : 9,8,7,6,5,4,3,2,1

(reverse = true) is the optional parameter. This parameter (reverse = true) will be passed when you need a list in descending order.

6. reverse() method

The reverse() method in python is used to reverse the elements of a given list. This method does not require any input parameter.

Syntax :

list.reverse()

Example code : 

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
list_1.reverse()

print(‘reversed list :’ , list_1)

Output :

reversed list : [‘star_city,’new_york’,’gotham_city’,’central_city’]

7. remove() method

The remove() method in python is used to remove an element from a list. This method requires an element to be removed as an input parameter.

Syntax :

list.remove(element)

Example code 1:

list_1 =  [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’] 
list_1.remove(‘gotham_city’)

print(‘updated list : ’, list_1)

Output :

Updated list :  [‘central_city’, ‘new_york’, ’star_city’]

Example code 2:

If an element appears multiple times in your list then remove() method will remove the first matching element from the list.

list_ 1 = [‘central_city’, ‘gotham_city’,’gotham_city’ ‘new_york’, ’star_city’,’gotham_city’]
list_1.remove(‘gotham_city’)

print(‘updated list: ’, list_1)

Output :

updated list : [‘central_city’,’gotham_city’ ‘new_york’, ’star_city’,’gotham_city’]

8. pop() method

The pop() method in python is used to pop an element from a list. The pop() method requires an index of the element to be removed as an input parameter. This method returns the element too.

Syntax :

list.pop(index)

Example code :

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’]
popped_city = list_1.pop(1)

print(‘the popped city from the list is : ’, popped_city)
print(‘the updated list : ’, list_1)

Output:

the popped city from the list is: gotham_city

the updated list : [‘central_city’, ‘new_york’, ’star_city’]

9. insert() method

The insert() method is used to insert an element in the list at the given index position. This method takes two input parameters, first is an index and the second is an element to be inserted.

Syntax :

list.insert(index, element)

Example Code :

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’] 

list_1.insert(1,’gorilla_city’)

print(‘updated list: ’, list_1)

Output :

Updated list :  [‘central_city’,’gorilla_city’, ‘gotham_city’, ‘new_york’, ’star_city’]

10. extend() method

The extend() method is used to extend a list. And this method will modify the previous list.

Syntax :

list.extend(list_1)

Example code :

list_ 1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’] 
list_2 = [‘argo_city’, ‘coast_list’]
list_1.extend(list_2)

print(‘the updated list : ’, list_1)

Output :

[‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’,’argo_city’, ‘coast_list’]

11. index() method

The index() method in python is used to find the index number of a given element.

Syntax :

list.index(element)

Example code :

list_1 = [‘central_city’, ‘gotham_city’, ‘new_york’, ’star_city’] 
i = list_1.index(‘gotham_city’)

print(‘the index of given element :’, i)

Output :

The index of given element : 1

Conclusion :

We have discussed a lot of python list methods that are used to perform some basic operations on the list. List methods are so important in python programming language because these methods can ease programming workload.

Suppose if you want to sort a simple list then you have to just apply the sort() function on the list and that function will sort your list.

 

Leave a Comment