List Data Structure in Python
Python lists are versatile data structures that allow you to store and manage collections of elements. Lists are mutable, meaning you can modify their contents, and they can hold a mixture of different data types. In this blog, we will explore various methods associated with Python lists, along with relevant examples to demonstrate their usage
Creating Lists:
To create a list in Python, you enclose elements within square brackets, separated by commas. Let’s start with a simple example:
# Creating a list of numbers
numbers_list = [1, 2, 3, 4, 5]
# Creating a list of strings
fruits_list = ['apple', 'banana', 'orange']
List Operations
Append(): The append()
method allows you to add an element to the end of the list.
# Example of append()
fruits_list = ['apple', 'banana', 'orange']
fruits_list.append('mango')
print(fruits_list)
# Output: ['apple', 'banana', 'orange', 'mango']
Insert(): The insert()
method enables you to add an element at a specific index within the list.
# Example of insert()
numbers_list = [1, 2, 3, 4, 5]
numbers_list.insert(2, 10)
print(numbers_list)
# Output: [1, 2, 10, 3, 4, 5]
Extend(): The extend()
method is used to add multiple elements from another iterable (list, tuple, etc.) to the end of the list.
# Example of extend()
numbers_list = [1, 2, 3]
new_numbers = [4, 5, 6]
numbers_list.extend(new_numbers)
print(numbers_list)
# Output: [1, 2, 3, 4, 5, 6]
Remove(): The remove()
method deletes the first occurrence of a specified element from the list.
# Example of remove()
fruits_list = ['apple', 'banana', 'orange', 'apple']
fruits_list.remove('apple')
print(fruits_list)
# Output: ['banana', 'orange', 'apple']
Pop(): The pop()
method removes the element at the given index and returns it. If no index is provided, it removes and returns the last element.
# Example of pop()
numbers_list = [1, 2, 3, 4, 5]
popped_element = numbers_list.pop(2)
print(numbers_list)
# Output: [1, 2, 4, 5]
print(popped_element)
# Output: 3
Index(): The index()
method returns the index of the first occurrence of a specified element in the list.
# Example of index()
fruits_list = ['apple', 'banana', 'orange']
index_of_banana = fruits_list.index('banana')
print(index_of_banana)
# Output: 1
Count(): The count()
method returns the number of occurrences of a given element in the list.
# Example of count()
numbers_list = [1, 2, 2, 3, 2, 4, 5]
count_of_twos = numbers_list.count(2)
print(count_of_twos)
# Output: 3
Sort(): The sort()
method sorts the elements of the list in ascending order. It modifies the original list and does not return a new list.
# Example of sort()
numbers_list = [5, 3, 1, 4, 2]
numbers_list.sort()
print(numbers_list)
# Output: [1, 2, 3, 4, 5]
Reverse(): The reverse()
method reverses the elements of the list in place.
# Example of reverse()
numbers_list = [1, 2, 3, 4, 5]
numbers_list.reverse()
print(numbers_list)
# Output: [5, 4, 3, 2, 1]
If you liked the content follow me on medium and connect on LinkedIn: https://www.linkedin.com/in/abhishek-varma-gollapalli/