Lists
A list is one of the most commonly used data types in Python. It is a collection of items that can hold multiple values in a single variable. Lists are ordered, mutable (changeable), and allow duplicate values.
Key Features of Lists
- Ordered: Items in a list have a specific order (index).
- Mutable: You can change the items in a list after creating it.
- Can Contain Different Data Types: A list can hold integers, strings, floats, or even other lists.
- Duplicates Allowed: A list can contain the same value multiple times.
Creating a List
You can create a list using square brackets [].
Examples:
Python
Copy
# Empty list empty_list = [] # List of numbers numbers = [1, 2, 3, 4, 5] # List of strings fruits = ["apple", "banana", "cherry"] # Mixed data types mixed_list = [1, "hello", 3.14] # Nested list (a list inside another list) nested_list = [[1, 2], [3, 4]]
Accessing List Items
You can access items in a list using indexing (starting from 0).
Examples:
Python
Copy
fruits = ["apple", "banana", "cherry"] # Access first item print(fruits[0]) # Output: apple # Access last item using negative index print(fruits[-1]) # Output: cherry
Modifying a List
Change an Item
Python
Copy
fruits = ["apple", "banana", "cherry"] fruits[1] = "orange" print(fruits) # Output: ['apple', 'orange', 'cherry']
Add Items
- Using append(): Adds an item to the end.
Python
Copy
fruits.append("grape") print(fruits) # Output: ['apple', 'orange', 'cherry', 'grape']
- Using insert(): Adds an item at a specific position.
Python
Copy
fruits.insert(1, "mango") print(fruits) # Output: ['apple', 'mango', 'orange', 'cherry']
Remove Items
- Using remove(): Removes the first occurrence of a value.
Python
Copy
fruits.remove("orange") print(fruits) # Output: ['apple', 'mango', 'cherry']
- Using pop(): Removes an item by index (default is the last item).
Python
Copy
fruits.pop(1) print(fruits) # Output: ['apple', 'cherry']
- Using del: Deletes an item by index or the entire list.
Python
Copy
del fruits[0] print(fruits) # Output: ['cherry']
Looping Through a List
You can use a for loop to iterate through all items in a list.
Example:
Python
Copy
fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit)
Output:
apple
banana
cherry
List Methods
Here are some common methods you can use with lists:
Method | Description | Example |
---|---|---|
append(x) | Adds an item to the end of the list | fruits.append("grape") |
insert(i, x) | Inserts an item at position i | fruits.insert(1, "mango") |
remove(x) | Removes the first occurrence of x | fruits.remove("apple") |
pop(i) | Removes the item at index i | fruits.pop(2) |
sort() | Sorts the list in ascending order | numbers.sort() |
reverse() | Reverses the order of the list | fruits.reverse() |
index(x) | Returns the index of the first occurrence | fruits.index("cherry") |
count(x) | Counts the occurrences of x | fruits.count("apple") |
extend(iterable) | Adds elements of an iterable to the list | fruits.extend(["kiwi", "melon"]) |
Slicing a List
You can extract a portion of a list using slicing.
Syntax:
list[start:end:step]
Examples:
Python
Copy
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Get items from index 2 to 5 (exclusive) print(numbers[2:6]) # Output: [2, 3, 4, 5] # Get items from the beginning to index 4 print(numbers[:5]) # Output: [0, 1, 2, 3, 4] # Get every second item print(numbers[::2]) # Output: [0, 2, 4, 6, 8]
Combining Lists
Concatenation
You can combine lists using the + operator.
Python
Copy
list1 = [1, 2] list2 = [3, 4] combined = list1 + list2 print(combined) # Output: [1, 2, 3, 4]
Repetition
You can repeat lists using the * operator.
Python
Copy
numbers = [1, 2] repeated = numbers * 3 print(repeated) # Output: [1, 2, 1, 2, 1, 2]