Python List has built-in methods that you can use for important operations in the list data structure. Python List function is changed from time to time in different versions.
The most basic and important data structure in Python is the List. In this tutorial, you will learn about the list methods of objects in Python 3.
Here are all of the methods of list objects:
List Function in python 3.
- append(x)
- extend(iterable)
- insert(i, x)
- remove(x)
- pop([i])
- clear()
- index(x[, start[, end]])
- count(x)
- sort(key=None, reverse=False)
- reverse()
- copy()
Examples of Python list methods
Append function – list.append(obj)
Add an element to the end of the list. Equivalent to a[len(a):] = [x]
.
Add the new number to a list.
aList = [1, 2, 3, 4] print(aList) aList.append(5) print("Updated List : ", aList)
Output : [1, 2, 3, 4]
Updated List : [1, 2, 3, 4, 5]
Read More:– Python List append() Method
Extend Function – list.extend(iterable)
Extend the list by appending all the items from the iterable.
Add the item of programming list to the language list:
# language list language = ['French', 'English', 'German'] # another list of programing language programing = ['python', 'Java'] language.extend(programing) print('Extended List: ', language)
Output: Extended List: [‘French’, ‘English’, ‘German’, ‘python’, ‘Java’]
Read More: Python list extend Function
Insert Function – list.insert(index, element)
It is used to Insert an item at a given position.
Adding a number 7 at position 0;
# List list1 = [1, 2, 3, 4, 5, 6] # Inserting value list1.insert(0, 7) print("New List: ", list1)
Output: New List: [7, 1, 2, 3, 4, 5, 6]
Read More: Python list insert Function
Remove Function – list.remove(element)
Remove the first item from the list.
Example of removes the first occurrence of 4 from the list.
list1 = [3, 4, 1, 1, 8, 9] list1.remove(4) print(list1)
Output: [3, 1, 1, 8, 9]
Read More: Python list remove() function
Pop function – list.pop(index)
Use it to Remove the item at the given position in the list, and return it.
Remove an element specified position using index value in the pop() function.
languages = ['Python', 'Java', 'C++', 'Kotlin'] # removing java print(languages.pop(1)) print(languages)
Output: Java
[‘Python’, ‘C++’, ‘Kotlin’]
Read More: Python pop() Function
Clear Function – list.clear()
Remove all items from the list.
oldlist = ["a", "b", "c", "d"] newList = oldlist.clear() print(newList)
Output: None
Read More: Python clear list
Index Function – list.index(element)
Return index of the list of the first item whose value matched.
finding the index of the element in the list python.
nums = [14, 5, 4, 5, 7, 32] x = nums.index(5) print(x)
Output: 1
Read More: Python list index Function
Count Function – list.count(element)
Get the occurrence of elements in the list.
Count the occurrence of an element in the list
# vowels list vowels = ['a', 'e', 'i', 'o', 'i', 'u'] # count element 'i' count = vowels.count('i')
Output: The count of i is: 2
Read More:
Sort Function – list.sort(reverse=True|False, key=myFunc)
Sort the items on the list.
Python sort list of strings.
# vowels list vowels = ['e', 'a', 'u', 'o', 'i'] # sort the vowels vowels.sort() # print vowels print('Sorted list:', vowels)
Output: Sorted list: [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
Read More: Python sort list (Array)
Reverse Function – list.reverse()
Reverse the elements of the list in place.
It will modify the original list.
list1 = [1, 4, 3, 6, 7] # Reversing List list1.reverse() print(list1)
Output: [7, 6, 3, 4, 1]
Read More: Python reverse list
Copy Function – list.copy()
Return a shallow copy of the list.
Copying the list of the fruits. It’s a copy list without changing the original list.
fruits = ['apple', 'banana', 'cherry', 'orange'] copy_fruits = fruits.copy() print(copy_fruits)
Output: [‘apple’, ‘banana’, ‘cherry’, ‘orange’]
Read More: Python list copy Function
Do comment if you have any doubts and suggestions on this tutorial.
Note:
IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.