Index
A list in Python is a collection of items that is ordered, mutable (changeable), and allows duplicate values. It is one of the most commonly used data structures in Python.
1. Creating a List
A list is created using square brackets []
, with elements separated by commas.
# Creating a list of numbers
numbers = [1, 2, 3, 4, 5]
# Creating a list of strings
fruits = ["apple", "banana", "cherry"]
# Creating a mixed list
mixed_list = [10, "Python", 3.14, True]
print(numbers) # Output: [1, 2, 3, 4, 5]
print(fruits) # Output: ['apple', 'banana', 'cherry']
print(mixed_list) # Output: [10, 'Python', 3.14, True]
2. Accessing List Elements
Each element in a list has an index (starting from 0).
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple (First element)
print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry (Last element)
✅ Slicing Lists:
You can extract a portion of the list using slicing.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # Output: [20, 30, 40] (From index 1 to 3)
print(numbers[:3]) # Output: [10, 20, 30] (First 3 elements)
print(numbers[2:]) # Output: [30, 40, 50] (From index 2 to end)
print(numbers[::-1]) # Output: [50, 40, 30, 20, 10] (Reverse list)
3. Modifying a List (Mutable)
Lists can be changed by assigning new values to specific indexes.
fruits = ["apple", "banana", "cherry"]
# Changing an element
fruits[1] = "grape"
print(fruits) # Output: ['apple', 'grape', 'cherry']
✅ Adding elements to a list:
fruits.append("orange") # Adds to the end
print(fruits) # Output: ['apple', 'grape', 'cherry', 'orange']
fruits.insert(1, "mango") # Inserts at index 1
print(fruits) # Output: ['apple', 'mango', 'grape', 'cherry', 'orange']
✅ Removing elements from a list:
fruits.remove("grape") # Removes by value
print(fruits) # Output: ['apple', 'mango', 'cherry', 'orange']
fruits.pop(2) # Removes by index
print(fruits) # Output: ['apple', 'mango', 'orange']
last_item = fruits.pop() # Removes the last element
print(last_item) # Output: orange
print(fruits) # Output: ['apple', 'mango']
✅ Deleting a list or an element:
del fruits[0] # Deletes 'apple'
print(fruits) # Output: ['mango']
del fruits # Deletes the entire list
✅ Clearing a list (removes all elements):
numbers = [1, 2, 3, 4, 5]
numbers.clear()
print(numbers) # Output: []
4. List Operations
✅ Concatenation (+
):
Lists can be joined together.
a = [1, 2, 3]
b = [4, 5, 6]
result = a + b
print(result) # Output: [1, 2, 3, 4, 5, 6]
✅ Repetition (*
):
Repeats elements in a list.
x = [10, 20]
print(x * 3) # Output: [10, 20, 10, 20, 10, 20]
✅ Checking if an item exists (in
):
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
print("grape" in fruits) # Output: False
5. Looping Through a List
✅ Using a for
loop:
Output:
apple
banana
cherry
✅ Using while
loop:
fruits = ["apple", "banana", "cherry"]
i = 0
while i < len(fruits):
print(fruits[i])
i += 1
6. Useful List Methods
Method | Description |
---|---|
append(x) | Adds x to the end of the list |
insert(i, x) | Inserts x at index i |
remove(x) | Removes the first occurrence of x |
pop(i) | Removes element at index i (default is last) |
clear() | Removes all elements from the list |
index(x) | Returns the index of x |
count(x) | Counts occurrences of x |
sort() | Sorts the list in ascending order |
reverse() | Reverses the list |
✅ Example of some methods:
numbers = [5, 2, 9, 1, 5, 6]
numbers.sort() # Sorting
print(numbers) # Output: [1, 2, 5, 5, 6, 9]
numbers.reverse() # Reversing
print(numbers) # Output: [9, 6, 5, 5, 2, 1]
print(numbers.count(5)) # Output: 2 (counts occurrences of 5)
7. Nested Lists (Lists Inside Lists)
A list can contain other lists (nested lists).
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # Output: 6 (Row index 1, Column index 2)