Python is a very popular programming language which provides different data types to organize data. One of the most used datatypes is List.
Python lists have these properties.
- Ordered: Python Lists store data in the same order as they are inserted, allowing access and updates using indices.
- Mutable: Lists are mutable by nature, meaning we can change, add, or remove elements after the list is created.
- Allows Duplicates: Lists can contain duplicate items, and each occurrence is treated as a separate item.
- Heterogeneous Elements: Python Lists can also store elements of different data types, such as int, str, nested, dict, float, or any other data types, all within the same list.
- Indexed Access: Elements in a list can be accessed using their index positions, starting from 0 for the first element and also negative indexing works like last element can be access by -1 index.
- Dynamic Size: Lists in Python can grow or shrink dynamically as elements are added or removed.
- Supports Slicing: Python lists support slicing to extract a specific item’s portion of a list, and to creating a new list with the extracted elements.
- Iterable: Lists can be iterated via using loops or comprehensions to perform operations on each element.
Syntax:
we can define python list with square brackets
example_list = []
or
example_list = ["Hello", "World", 1, 2.0]
Let’s have a look on some examples
# Below python list with 5 string variables example_list = ["Cricket", "Badminton", "Hockey", "Kabaddi", "Football"] print(example_list)
Output:
example_list = [“Cricket”, “Badminton”, “Hockey”, “Kabaddi”, “Football”]
To working with python list there are few inbuilt methods available let’s check them one by one:
append(item):
- The
append()
method is used to add a single element to the end of the list. It modifies the original list in place.
numbers = [11, 22, 33] numbers.append(40) print(numbers) # Output: [11, 22, 33, 40]
extend(iterable):
- The
extend()
method is similar toappend()
, but it takes an iterable (such as another list) as an argument and adds each element of the iterable to the end of the list.
numbers = [1, 2, 3] numbers.extend([4, 5]) print(numbers) # Output: [1, 2, 3, 4, 5]
insert(index, item):
- The
insert()
method is used to insert an element at a specified index in the list. Elements at and after the specified index are shifted to the right.
numbers = [1, 2, 3] numbers.insert(1, 10) print(numbers) # Output: [1, 10, 2, 3]
remove(item):
- The
remove()
method removes the first occurrence of the specified item from the list. If the item is not found, it raises a ValueError.
numbers = [1, 2, 3, 2] numbers.remove(2) print(numbers) # Output: [1, 3, 2]
pop(index):
- The
pop()
method removes and returns the element at the specified index. If index value is not provided in pop method, then it removes and returns the last element of the list.
numbers = [1, 2, 3] popped_item = numbers.pop(1) print(popped_item) # Output: 2 print(numbers) # Output: [1, 3]
index(item):
- The
index()
method returns the index of the first occurrence of the specified item in the list. If the item is not found, it raises a ValueError.
numbers = [10, 20, 30, 20] index = numbers.index(20) print(index) # Output: 1
count(item):
- The
count()
method returns the number of occurrences of the specified item in the list.
numbers = [1, 2, 3, 2, 1, 2] count = numbers.count(2) print(count) # Output: 3
sort():
- The
sort()
method is used to sort the elements of the list in ascending order. It modifies the original list
numbers = [5, 2, 7, 1] numbers.sort() print(numbers) # Output: [1, 2, 5, 7]
reverse():
- The
reverse()
method reverses the order of elements in the list in place.
numbers = [1, 2, 3] numbers.reverse() print(numbers) # Output: [3, 2, 1]
clear():
- The
clear()
method removes all elements from the list, leaving it empty.
numbers = [1, 2, 3] numbers.clear() print(numbers) # Output: []