Python Lists offers a wide range of built-in methods to manipulate data efficiently. List in Python, is an ordered and mutable data type allowing us to store multiple values in a single variable.
Let us take a look at various List methods available in Python.
| Method | Description |
|---|---|
| append() | This method is utilized to add an element x to the end of the list. |
| extend() | This method is utilized to add all elements of an iterable (list, tuple, etc.) to the list. |
| insert() | This method is utilized to insert an element x at index i. |
| remove() | This method is utilized to remove the first occurrence of x. It raises ValueError if x is not found. |
| pop() | This method is utilized to remove and returns the element at index i (default is the last element). |
| clear() | This method is utilized to remove all elements, making the list empty. |
| index() | This method is utilized to return the first index of x between start and end. It raises ValueError if not found. |
| count() | This method is utilized to return the number of occurrences of x in the list. |
| sort() | This method is utilized to sort the list in place (default is ascending). |
| reverse() | This method is utilized to reverse the order of the list in place. |
| copy() | This method is utilized to return a shallow copy of the list. |
1) append()
The append() method in Python allows us to add an element at the end of the list.
Syntax:
It has the following syntax:
list_name.append(item)
Python append() Method Example
Let us take an example to demonstrate the append() method in Python.
Example
# python program to show the use of list append() method
# creating a list
list_of_fruits = ['apple', 'mango', 'banana', 'orange', 'guava']
# printing the list
print("List of Fruits:", list_of_fruits)
# using the append() method
list_of_fruits.append('grapes')
# printing the updated list
print("Updated List of Fruits:", list_of_fruits)
Output:
List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava']
Updated List of Fruits: ['apple', 'mango', 'banana', 'orange', 'guava', 'grapes']
Explanation:
In the above example, we have used the append() method to add a new element ‘grapes’ at the end of the given list.
2) extend()
The extend() method in Python allows us to append all the elements from an iterable (list, tuple, set, etc.) at the end of the list.
Syntax:
It has the following syntax:
list_name.extend(iterable)
Python extend() Method Example
Let us take an example to demonstrate the extend() method in Python.
Example
# python program to show the use of list extend() method
# initializing the lists
shopping_list = ['milk', 'bread', 'butter']
new_items = ['eggs', 'apples', 'coffee']
# printing the lists
print("Old Shopping List:", shopping_list)
print("New Items:", new_items)
# using the extend() method
shopping_list.extend(new_items)
# printing the updated list
print("Updated Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'butter']
New Items: ['eggs', 'apples', 'coffee']
Updated Shopping List: ['milk', 'bread', 'butter', 'eggs', 'apples', 'coffee']
Explanation:
In the above example, we have used the extend() method to add an iterable at the end of the given list.
3) insert()
The insert() method in Python allows us to insert an the elements at a specified position in the list.
Syntax:
It has the following syntax:
list_name.insert(index, item)
Python insert() Method Example
Let us take an example to illustrate how the insert() method works in Python.
Example
# python program to show the use of list insert() method
# initializing the list
shopping_list = ['milk', 'bread', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the insert() method
shopping_list.insert(2, 'eggs')
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'butter', 'coffee']
New Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
Explanation:
In the above example, we have used the insert() method to insert item ‘eggs’ at the second index of the given list.
4) remove()
The remove() method in Python allows us to remove the first occurrence of the specified element from the list.
Syntax:
It has the following syntax:
list_name.remove(item)
Python remove() Method Example
Let us take an example to demonstrate the remove() method in Python.
Example
# python program to show the use of list remove() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the remove() method
shopping_list.remove('butter')
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: ['milk', 'bread', 'eggs', 'coffee']
Explanation:
In the above example, we have used the remove() method to remove item ‘butter’ from the given list.
5) pop()
The pop() method in Python allows us to remove and return the element from the specified index of the given list.
Syntax:
It has the following syntax:
list_name.pop(index)
Python pop() Method Example
Let us take an example to demonstrate the pop() method in Python.
Example
# python program to show the use of list pop() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the pop() method
popped_item = shopping_list.pop(1)
# printing the updated list
print("New Shopping List:", shopping_list)
print("Popped Item:", popped_item) # popped element
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: ['milk', 'eggs', 'butter', 'coffee']
Popped Item: bread
Explanation:
In the above example, we have used the pop() method to remove and return the item at index ‘2’ from the given list.
6) clear()
The clear() method in Python allows us to remove all the elements from the list making it empty.
Syntax:
It has the following syntax:
list_name.clear()
Python clear() Method Example
Let us take an example to demonstrate the clear() method in Python.
Example
# python program to show the use of list clear() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the list
print("Old Shopping List:", shopping_list)
# using the clear() method
shopping_list.clear()
# printing the updated list
print("New Shopping List:", shopping_list)
Output:
Old Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
New Shopping List: []
Explanation:
In the above example, we have used the clear() method to remove all the elements from the given list.
7) index()
The index() method in Python returns the first index of the element within start and end.
Syntax:
It has the following syntax:
list_name.index(item, start, end)
Python index() Method Example
Let us take an example to demonstrate the index() method in Python.
Example
# python program to show the use of list index() method
# initializing the list
shopping_list = ['milk', 'bread', 'eggs', 'butter', 'coffee']
# printing the lists
print("Shopping List:", shopping_list)
# using the index() method
item_index = shopping_list.index('butter')
# printing the index of the item
print("Index of butter:", item_index)
Output:
Shopping List: ['milk', 'bread', 'eggs', 'butter', 'coffee']
Index of butter: 3
Explanation:
In the above example, we have used the index() method to find the first occurrence of the element from the given list.
8) count()
The count() method in Python allows us to all the occurrence of the element in the list.
Syntax:
It has the following syntax:
list_name.count(item)
Python count() Method Example
Let us take an example to demonstrate the count() method in Python.
Example
# python program to show the use of list count() method
# initializing the list
shopping_list = ['milk', 'bread', 'egg', 'milk', 'butter', 'coffee', 'egg', 'flour', 'egg']
# printing the lists
print("Shopping List:", shopping_list)
# using the count() method
item_count = shopping_list.count('egg')
# printing the count of the item
print("No. of Eggs:", item_count)
Output:
Shopping List: ['milk', 'bread', 'egg', 'milk', 'butter', 'coffee', 'egg', 'flour', 'egg']
No. of Eggs: 3
Explanation:
In the above example, we have used the count() method to find all the occurrences of ‘egg’ in the given list.
9) sort()
The sort() method in Python allows us to sort the list in place.
Syntax:
It has the following syntax:
list_name.sort(reverse = False, key = None)
Python sort() Method Example
Let us take an example to illustrate how the sort() method works in Python.
Example
# python program to show the use of list sort() method
# initializing the list
fruit_basket = ['mango', 'apple', 'orange', 'banana', 'grapes']
# printing the list
print("Unsorted Fruit Basket:", fruit_basket)
# using the sort() method
fruit_basket.sort()
# printing the updated list
print("Sorted Fruit Basket:", fruit_basket)
Output:
Unsorted Fruit Basket: ['mango', 'apple', 'orange', 'banana', 'grapes']
Sorted Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the above example, we have used the sort() method to sort the elements of the given list in ascending order.
10) reverse()
The reverse() method in Python allows us to reverse the list in place.
Syntax:
It has the following syntax:
list_name.reverse()
Python reverse() Method Example
Let us take an example to illustrate how the reverse() method works in Python.
Example
# python program to show the use of list reverse() method
# initializing the list
fruit_basket = ['apple', 'banana', 'grapes', 'mango', 'orange']
# printing the list
print("Fruit Basket:", fruit_basket)
# using the reverse() method
fruit_basket.reverse()
# printing the updated list
print("Reversed Fruit Basket:", fruit_basket)
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Reversed Fruit Basket: ['orange', 'mango', 'grapes', 'banana', 'apple']
Explanation:
In the above example, we have used the reverse() method to reverse the given list.
11) copy()
The copy() method in Python allows us to create a shallow copy of the list.
Syntax:
It has the following syntax:
list_name.copy()
Python copy() Method Example
Let us take an example to illustrate how the copy() method works in Python.
Example
# python program to show the use of list copy() method
# initializing the list
fruit_basket = ['apple', 'banana', 'grapes', 'mango', 'orange']
# printing the list
print("Fruit Basket:", fruit_basket)
# using the copy() method
new_fruit_basket = fruit_basket.copy()
# printing the copied list
print("Copied Fruit Basket:", new_fruit_basket)
Output:
Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Copied Fruit Basket: ['apple', 'banana', 'grapes', 'mango', 'orange']
Explanation:
In the above example, we have used the copy() method to create a shallow copy of the given list.
Leave a Reply