Lists in Python
A list is a collection used to store multiple values in a single variable.
Lists are one of the most commonly used data types in Python.
Example
fruits = ["Apple", "Banana", "Mango"]
Here, fruits is a list containing three items.
1. Creating a List
Lists are created using square brackets [].
numbers = [10, 20, 30, 40] names = ["Ram", "Shyam", "Hari"] mixed = [10, "Python", 5.5, True]
A list can contain different data types.
2. Accessing List Items
List indexing starts from 0.
fruits = ["Apple", "Banana", "Mango"]
Apple Banana Mango 0 1 2
Example:
print(fruits[0]) print(fruits[1]) print(fruits[2])
Output:
Apple Banana Mango
3. Negative Indexing
Negative indexes start from the end.
Apple Banana Mango -3 -2 -1
Example:
fruits = ["Apple", "Banana", "Mango"] print(fruits[-1]) print(fruits[-2])
Output:
Mango Banana
4. Changing List Items
Lists are mutable, meaning their items can be changed.
fruits = ["Apple", "Banana", "Mango"] fruits[1] = "Orange" print(fruits)
Output:
['Apple', 'Orange', 'Mango']
5. Adding Items
append()
Adds an item to the end of the list.
fruits = ["Apple", "Banana"] fruits.append("Mango") print(fruits)
Output:
['Apple', 'Banana', 'Mango']
insert()
Adds an item at a specific position.
fruits = ["Apple", "Mango"] fruits.insert(1, "Banana") print(fruits)
Output:
['Apple', 'Banana', 'Mango']
6. Adding Multiple Items
extend()
Adds multiple items to a list.
fruits = ["Apple", "Banana"] fruits.extend(["Mango", "Orange"]) print(fruits)
Output:
['Apple', 'Banana', 'Mango', 'Orange']
7. Removing Items
remove()
Removes a specific value.
fruits = ["Apple", "Banana", "Mango"] fruits.remove("Banana") print(fruits)
Output:
['Apple', 'Mango']
pop()
Removes an item using its index.
fruits = ["Apple", "Banana", "Mango"] fruits.pop(1) print(fruits)
Output:
['Apple', 'Mango']
Without an index, pop() removes the last item:
fruits.pop()
del
You can also delete an item using del.
fruits = ["Apple", "Banana", "Mango"] del fruits[1] print(fruits)
8. List Slicing
Slicing is used to get part of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Output:
[20, 30, 40]
Other examples:
print(numbers[:3]) print(numbers[2:]) print(numbers[:])
Output:
[10, 20, 30] [30, 40, 50] [10, 20, 30, 40, 50]
9. Finding the Length
Use len():
fruits = ["Apple", "Banana", "Mango"] print(len(fruits))
Output:
3
10. Checking an Item
Use in:
fruits = ["Apple", "Banana", "Mango"] print("Apple" in fruits)
Output:
True
Using not in:
print("Orange" not in fruits)
Output:
True
11. Sorting a List
sort()
Sorts the list in ascending order.
numbers = [40, 10, 30, 20] numbers.sort() print(numbers)
Output:
[10, 20, 30, 40]
For descending order:
numbers.sort(reverse=True) print(numbers)
Output:
[40, 30, 20, 10]
12. Reversing a List
Use reverse():
numbers = [10, 20, 30, 40] numbers.reverse() print(numbers)
Output:
[40, 30, 20, 10]
13. Counting Items
Use count():
numbers = [10, 20, 10, 30, 10] print(numbers.count(10))
Output:
3
14. Finding an Item's Position
Use index():
fruits = ["Apple", "Banana", "Mango"] print(fruits.index("Banana"))
Output:
1
15. Copying a List
Use copy():
fruits = ["Apple", "Banana", "Mango"] new_fruits = fruits.copy() print(new_fruits)
This creates a separate copy of the list.
16. Clearing a List
Use clear():
fruits = ["Apple", "Banana", "Mango"] fruits.clear() print(fruits)
Output:
[]
17. Loop Through a List
Lists are commonly used with loops.
fruits = ["Apple", "Banana", "Mango"] for fruit in fruits: print(fruit)
Output:
Apple Banana Mango
18. List with Numbers
marks = [85, 90, 75, 88, 92] print("Total:", sum(marks)) print("Highest:", max(marks)) print("Lowest:", min(marks))
Output:
Total: 430 Highest: 92 Lowest: 75
19. Nested Lists
A list can contain another list.
students = [ ["Ram", 20], ["Shyam", 21], ["Hari", 19] ]
Access the data:
print(students[0])
Output:
['Ram', 20]
Access a specific value:
print(students[0][0])
Output:
Ram
20. List Comprehension
List comprehension provides a short way to create lists.
Normal approach:
numbers = [] for i in range(1, 6): numbers.append(i) print(numbers)
Using list comprehension:
numbers = [i for i in range(1, 6)] print(numbers)
Output:
[1, 2, 3, 4, 5]
With a condition:
even_numbers = [i for i in range(1, 11) if i % 2 == 0] print(even_numbers)
Output:
[2, 4, 6, 8, 10]
Important List Methods
| Method | Purpose |
|---|---|
append() | Add item at the end |
insert() | Add item at a position |
extend() | Add multiple items |
remove() | Remove a specific item |
pop() | Remove item by index |
clear() | Remove all items |
sort() | Sort the list |
reverse() | Reverse the list |
copy() | Copy the list |
count() | Count an item |
index() | Find an item's position |
Important List Functions
| Function | Purpose |
|---|---|
len() | Number of items |
max() | Largest value |
min() | Smallest value |
sum() | Total of numbers |
list() | Convert to a list |
List Characteristics
A Python list is:
- Ordered — items maintain their position.
- Mutable — items can be changed.
- Allows duplicates — the same value can appear multiple times.
- Allows different data types — one list can contain different types.
- Indexed — items can be accessed using indexes.
Example
student = ["Ram", 20, "CSE", 85.5]
A list is especially useful when you need to store and manage multiple related values in one variable.