Python list methods with examples

Python provides a lot of methods to work with Python List. Here are a few Python list methods with examples.

SL.Method NameDescription
1append()The append() method is used to add an item to the end of an existing list.
2extend()It is used to add multiple items to the end of a Python list.
3insert()This method is used to insert an element to a Python list at a specified index.
4remove()It is used to remove the first occurrence of a specified element from the list.
5clear()This method is used to remove all elements from a Python list.
6copy()This method is used to create a shallow copy of a given Python list.
7index()It is used to find the index of an element in a Python list.
8reverse()The reverse() method is used to reverse the order of elements in a list object
9count()This method is used to count the number of occurrences of a specified element within a list.
10pop()The pop() method is used to remove and returns the last item in the Python list by default.
11sort()The sort() method is used to sort the Python list in ascending or descending order.
12len()This method returns the number of items in the Python list.
Python list methods

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

append()

To add a single element at the end of the list, use the Python append() method:

cities.append('Phoenix')
print(cities)

extend()

To add multiple elements to the list, use the extend() method in Python:

cities.extend(['San Antonio', 'San Diego', 'Dallas'])
print(cities)

insert()

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

remove()

To remove an element by value from a list in Python, use the remove() method:

cities.remove('Houston')
print(cities)

pop()

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

Indexing

Access a Python list element by its index:

print(cities[1])

Slicing

Access a range of elements in a Python list using slicing:

print(cities[1:4])

Sorting in List

sort()

To sort the list in-place, use the sort() method:

cities.sort()
print(cities)

sorted()

To get a sorted copy of the list without modifying the original list, use the sorted() function:

sorted_cities = sorted(cities)
print(sorted_cities)

Reversing

reverse()

To reverse the list in-place, use the reverse() method:

cities.reverse()
print(cities)

Count Elements

count()

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)

Find Elements

index()

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.