Python Tutorial
List Methods in Python Explained With Example
Table of Contents
- Understanding List Methods in Python
- Quiz!
Understanding List Methods in Python
All the Python List methods are as listed below:
1. clear(): To delete all items from the list.
Code:
f = ["python", "web", "wscube"]
f.clear()
print(f)
Output:
[]
[]
2. copy(): To return a shallow copy of the list.
Code:
f = ["python", "web", "wscube"]
X = f.copy()
print(X)
Output:
["python", "web", "wscube"]
["python", "web", "wscube"]
3. count(): To return the count of the element in the list.
Code:
f = ["python", "web", "wscube"]
x = f.count("web")
print(x)
Output:
1
4. extend(): This function is used to ad iterable items to the end of the list.
Code:
f = ["python", "web", "wscube"]
cars = ['Ford', 'BMW', 'Volvo']
x= f.extend(cars)
print(x)
Output:
["python", "web", "wscube", ['Ford', 'BMW', 'Volvo'] ]
["python", "web", "wscube", ['Ford', 'BMW', 'Volvo'] ]
5. index(): To return the index of the element in the list.
Code:
f = ["python", "web", "wscube"]
x= f.index("web")
print(x)
Output:
2
2
6. reverse(): Reverses the list.
Code:
f = ["python", "web", "wscube"]
f.reverse()
print(f)
Output:
["wscube", "web","python"]
["wscube", "web","python"]
7. sort(): Sorts elements of a list
Code:
c = [1,3,2,7,5]
c.sort()
print(c)
Output:
[1, 2, 3, 5, 7]
[1, 2, 3, 5, 7]
Quiz!
