It is an ordered, mutable collection of elements.
Key Features of Lists in Python:
1. Ordered Collection: - Lists maintain the order of elements.
The position of each element in the list is determined by its index, which starts at `0` for the first element.
Example:
my_list = [10, 20, 30]
print(my_list[0])
Output: 10
print(my_list[1])
Output: 20
2. Mutable: - Lists are mutable, meaning that their elements can be changed after the list has been created.
You can modify, add, or remove elements from a list.
my_list = [1, 2, 3]
my_list[1] = 10 # Change the second element to 10
print(my_list) # Output: [1, 10, 3]
3. Allows Heterogeneous Elements: - A list can store elements of different data types.
For example, you can have a mix of integers, strings, floats, and even other lists as elements.
my_list = [1, "apple", 3.14, [1, 2, 3]]
print(my_list) # Output: [1, 'apple', 3.14, [1, 2, 3]]
4. Supports Nesting: - Lists can contain other lists as elements.
This means you can have a list of lists(nested lists).
nested_list = [[1, 2], [3, 4], [5, 6]]
print(nested_list[1]) # Output: [3, 4]
5. Indexing and Slicing: - You can access individual elements in a list using indexing (with `[]`), where the index starts at `0`.
Negative indexing is also supported, allowing access from the end of the list.
- You can extract a slice(sublist) using the slicing syntax `list[start:end]`, where `start` is inclusive and `end` is exclusive.
my_list = [10, 20, 30, 40, 50]
print(my_list[1:4]) # Output: [20, 30, 40] (Elements from index 1 to 3)
print(my_list[-1]) # Output: 50 (Last element)
6. Dynamic Size:- Lists in Python are dynamic in size, meaning that you can add or remove elements from a list without having to define a fixed size at the time of list creation.
my_list = [1, 2, 3]
my_list.append(4) # Adding an element to the end
print(my_list) # Output: [1, 2, 3, 4]
7. Common List Operations: - Python provides a wide range of methods and operations that can be performed on lists, such as adding, removing, and modifying elements, as well as finding the length and checking for membership.
-
Adding elements: - `append(item)`: Adds an item to the end of the list.
`insert(index, item)`: Inserts an item at a specified index.
`extend(iterable)`: Extends the list by appending all items from an iterable.
Examples:
my_list = [1, 2]
my_list.append(3) # Adds 3 to the end of the list
print(my_list) # Output: [1, 2, 3]
my_list.insert(1, 10) # Inserts 10 at index 1
print(my_list) # Output: [1, 10, 2, 3]
my_list.extend([4, 5]) # Adds all elements of [4, 5] to the list
print(my_list) # Output: [1, 10, 2, 3, 4, 5]
Removing elements:
`remove(item)`: Removes the first occurrence of an item from the list.
`pop(index)`: Removes and returns the item at the specified index.
`clear()`: Removes all items from the list.
Examples:
my_list = [1, 2, 3, 4]
my_list.remove(3) # Removes the first occurrence of 3
print(my_list) # Output: [1, 2, 4]
my_list.pop(1) # Removes and returns the item at index 1
print(my_list) # Output: [1, 4]
Other useful methods:
`len(list)`: Returns the number of elements in the list.
`index(item)`: Returns the index of the first occurrence of the item.
`count(item)`: Returns the number of times an item appears in the list.
`sort()`: Sorts the list in place.
`reverse()`: Reverses the order of elements in the list.
my_list = [3, 1, 2, 4]
print(len(my_list)) # Output: 4 (Length of the list)
my_list.sort() # Sorts the list
print(my_list)
# Output: [1, 2, 3, 4]
8.List Comprehensions: - Python provides a powerful and concise way to create lists using
list comprehensions , which allow you to generate new lists by applying an expression to each item in an existing iterable.
# Create a list of squares
squares = [x**2 for x in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
# Filter elements in a list (only even numbers)
even_numbers = [x for x in range(10) if x % 2 == 0]
print(even_numbers) # Output: [0, 2, 4, 6, 8]
Examples of List Operations:
1.Creating a List:
fruits = ["apple", "banana", "cherry"]
2. Accessing Elements:
print(fruits[0]) # Output: "apple"
print(fruits[-1]) # Output: "cherry" (last item)
3. Modifying Elements:
fruits[1] = "orange" # Change 'banana' to 'orange'
print(fruits) # Output: ["apple", "orange", "cherry"]
4. Adding Elements:
fruits.append("grape") # Adds 'grape' at the end
print(fruits) # Output: ["apple", "orange", "cherry", "grape"]
5. Removing Elements:
fruits.remove("orange") # Removes 'orange' from the list
print(fruits) # Output: ["apple", "cherry", "grape"]
6.Slicing a List:
print(fruits[1:3]) # Output: ["cherry", "grape"]
7. List Length:
print(len(fruits)) # Output: 3
Conclusion: A list in Python is a versatile and powerful data structure used to store and manipulate an ordered collection of elements. Lists are mutable, allowing modification after creation, and they support a wide range of operations like indexing, slicing, adding, removing, and sorting elements. Lists can contain elements of different types, and they allow nested collections, making them highly useful for a wide variety of tasks in Python.