Python provides a lot of methods to work with Python List. Here are a few Python list methods with examples.
|1||append()||The append() method is used to add an item to the end of an existing list.|
|2||extend()||It is used to add multiple items to the end of a Python list.|
|3||insert()||This method is used to insert an element to a Python list at a specified index.|
|4||remove()||It is used to remove the first occurrence of a specified element from the list.|
|5||clear()||This method is used to remove all elements from a Python list.|
|6||copy()||This method is used to create a shallow copy of a given Python list.|
|7||index()||It is used to find the index of an element in a Python list.|
|8||reverse()||The reverse() method is used to reverse the order of elements in a list object|
|9||count()||This method is used to count the number of occurrences of a specified element within a list.|
|10||pop()||The pop() method is used to remove and returns the last item in the Python list by default.|
|11||sort()||The sort() method is used to sort the Python list in ascending or descending order.|
|12||len()||This method returns the number of items in the Python list.|
Python lists are versatile and widely used data structures. They are ordered, mutable, and can store different data types.
Here is a Python list example.
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Philadelphia'] print(cities)
Adding elements to a Python List
To add a single element at the end of the list, use the Python append() method:
To add multiple elements to the list, use the extend() method in Python:
cities.extend(['San Antonio', 'San Diego', 'Dallas']) print(cities)
To insert an element at a specific index in a list, use the insert() method:
cities.insert(3, 'Miami') print(cities)
Removing Elements from a List in Python
To remove an element by value from a list in Python, use the remove() method:
To remove an element by index from a list and return its value, use the pop() method:
removed_city = cities.pop(2) print(removed_city) print(cities)
Accessing Elements from a Python List
Access a Python list element by its index:
Access a range of elements in a Python list using slicing:
Sorting in List
To sort the list in-place, use the sort() method:
To get a sorted copy of the list without modifying the original list, use the sorted() function:
sorted_cities = sorted(cities) print(sorted_cities)
To reverse the list in-place, use the reverse() method:
To count the occurrences of an element in a python list, use the count() method.
count_new_york = cities.count('New York') print(count_new_york)
To find the index of the first occurrence of an element in the list, use the index() method:
index_los_angeles = cities.index('Los Angeles') print(index_los_angeles)
This tutorial covered various Python list methods, including creating, adding, removing, accessing, sorting, reversing, counting, and finding elements. By using these methods, you can now manipulate lists in Python with ease.
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.