In this article you will learn about python list methods
Python List Method – Before moving ahead, let’s know a little bit about Python List
Here’s the list of Python List() Method –
Method Name | Description |
append() | It adds an element at the end of the list |
clear() | It removes the all elements of the list |
copy() | It makes a copy of original list |
count() | It returns the number of elements of specified value |
extend() | It adds the whole list at the end of list |
index() | It returns the index number of specified element |
insert() | It inserts an element at a particular place |
pop() | It removes the last element of list by default |
remove() | It removes the specified element from the list |
reverse() | It reverses the elements of the list |
sort() | It sorts the list. In other words, sets elements by alphabetically |
Let’s start with examples –
append() – It adds an element at the end of the list
Syntax – list.append(element) Parameter Values - Element – It is required. It includes element of any type like string, number, object etc.
Example 1- Add element at the end position.
x = ['Welcome' , 'in' , 'Python'] print('before adding:' , x) x.append('language') print('After adding:' , x)
Example – 2 Add a whole list as an element.
x = ['Welcome' , 'in' , 'Python'] print('before adding:' , x) x.append['language' , to learn it]) print('After adding:' , x)
clear() method – It removes the all elements of the list.
Syntax – list.clear() Parameter Values – No Parameter Values
Example – removes all elements of list
x = ['Welcome' , 'in' , 'Python'] print('before removing elements:' , x) x.clear() print('After removing elements:' , x)
copy() – It makes a copy of original list
Syntax – list.copy() Parameter Values – No Parameter Values
Example – Use of copy() method to copy a list.
x = ['Python', 'Java', 'HTML'] z = x.copy() print(x)
count() – It returns the number of elements of specified value.
Syntax – list.copy() Parameter Values – Value – It is required value and defined as the value to search for. It can be any type of value like (string, number, list, tuple, etc.).
Example 1- Use of count() method to count specified value (alphabet).
x = 'language' z = x.count('a') print(z)
Example 2- Use of count() method to count specified value (number).
x = [1 , 2 , 3 , 1 , 5 , 1 , 6 , 1] z = x.count(1) print(z)
extend() – It adds the whole list at the end of list
Syntax – list.extend(elements) Parameter Values – Elements – It is Required. The element (list, tuple, set etc.) or list of elements to be added.
Example 1 – Adding an element.
x = ['Python' , 'Java' , 'HTML'] x.extend(['C++']) print(x)
Example 2 – Adding a list of elements.
x = ['Python' , 'Java' , 'HTML'] x.extend(['C++', 'Java', 'C#']) print(x)
index() – It returns the index number of first occurrence specified element.
Syntax – list.index(element, value, value) Parameter Values – element – It is Required. The element to be searched for index number.
Example 1- Using index() method to get index number of particular element.
x = ['a', 'b', 'c', 'd'] print(x.index('c'))
Example 2 – Using index() method to get index number of first occurrence specified element between range.
x = ['a' , 'c' , 'g' , 'd' , 'c' , 't'] print(x.index('c' , 0 , 5))
Note: Index number starts from 0.
insert() – It inserts an element at a particular place.
Syntax – list.insert(value, element) Value – It is Required. A value where element is to be inserted. Element – It is Required. It is element that to be inserted.
Example 1 – Inserting ‘JavaScript’ at index number 2.
x = ['Python' , 'Java' , 'HTML'] x.insert(2 , 'JavaScript') print(x)
pop() – It removes the last element of list by default.
Syntax – list.pop(value) Parameter Value – Value – It is optional. A number specifying which element has to remove from list. By default, it removes last element of list that’s index number is -1.
Example 1- Using pop() method to remove last element.
x = ['Python' , 'Java' , 'HTML'] print("before removing:" , x) x.pop() print("After removing:" , x)
Example 2- Using pop() method to remove specified element.
x = ['Python' , 'Java' , 'HTML'] print("before removing:" , x) x.pop(1) print("After removing:" , x)
remove() – It removes the specified element from the list.
Syntax – list.remove(element) Parameter Value - Element – It is Required. It can be any type of element like string, number, list etc. that has to remove.
Example 1- Using remove() method to remove any element.
x = ['Python' , 'Java' , 'HTML'] x.remove('Java') print(x)
Example 2- showing an error while removing element.
x = ['Python' , 'Java' , 'HTML'] x.remove('c') print(x)
reverse() – It reverses the elements of the list.
Syntax – list.reverse() Parameter Values – No Values
Example – It reversed order of element of list.
x = ['a' , 'b' , 'c' , 'd'] print('before reversing:' , x) x.reverse() print("after reversing:" , x)
sort() – It sorts the list. In other words, sets elements by alphabetically.
Syntax - list.sort(reverse = True|False, key) Parameter Values – reverse - It is an optional argument. By default, it will sort the list in ascending order, otherwise reverse =True will sort in descending order. Key – It is Optional. It is a function describes sorting criteria.
Example 1- Using sort() method to sort the list.
x = [2 , 5 , 1 , 3] print("before sort:" , x) x.sort() print("after sort" , x)
Example 2- Using sort() method to sort the list by counting the length of element.
def myfunc(name): return len(name) name = ['Python' , 'Java' , 'C++'] name.sort(key=myfunc) print(name)
If you find anything incorrect in the above-discussed topic and have any further questions, please comment down below.
Like us on