Python | List Methods

Python | List Methods

Lists are one of the most commonly used data structures in Python, representing an ordered collection of items. Python provides several methods for manipulating lists, and in this article, we will discuss some of the most commonly used list methods.

1) len(): This is built-in function which returns the number of items in a list.
For example:

a = [1, 2, 3, 4, 5]
print(len(a))  # 5

2) list.append(): This method adds an item to the end of a list.
For example:


a = [1, 2, 3, 4, 5]
a.append(6)
print(a) # [1, 2, 3, 4, 5, 6]

3) list.extend(): This method adds multiple items to the end of a list.
For example:

a = [1, 2, 3, 4, 5]
a.extend([6, 7, 8])
print(a) # [1, 2, 3, 4, 5, 6, 7, 8]

4) list.insert(): This method inserts an item at a specified index in a list.
For example:

a = [1, 2, 3, 4, 5]
a.insert(3, 6)
print(a) # [1, 2, 3, 6, 4, 5]

5) list.remove(): This method removes the first occurrence of a specified item in a list.
For example:

a = [1, 2, 3, 4, 5]
a.remove(3)
print(a) # [1, 2, 4, 5]

6) list.pop(): This method removes and returns the item at a specified index in a list. If no index is specified, it removes and returns the last item.
For example:

a = [1, 2, 3, 4, 5]
a.pop(2) # 3
print(a) # [1, 2, 4, 5]

7) list.index(): This method returns the index of the first occurrence of a specified item in a list.
For example:

a = [1, 2, 3, 4, 5]
a.index(3) # 2

8) list.count(): This method returns the number of occurrences of a specified item in a list.
For example:

a = [1, 2, 3, 4, 5, 3, 3]
a.count(3) # 3

9) list.sort(): This method will sort the elements of the list in ascending order (by default).
For example:

a = [1, 2, 3, 4, 5, 3, 3]
a.sort() 
print(a) # [1, 2, 3, 3, 3, 4, 5]

In conclusion, these are just a few of the most commonly used list methods in Python. By using these methods, we can manipulate lists with ease and perform common operations such as adding, removing, and finding items in a list. Understanding and using these methods is an important part of working with lists in Python.

1 Comment

  1. Great article! It provides a clear explanation of the most commonly used list methods in Python. Understanding and utilizing these methods is essential for efficient list manipulation. Thanks for sharing!

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds