Introduction
When working with Python, we often need to store multiple values in one place.
For example, imagine you want to store the names of five students. You could create five separate variables, but that would make your code difficult to manage.
Python provides something called a List to solve this problem.
A list allows us to store multiple values in a single variable.
1. What is a List?
A list is a built-in Python data type used to store multiple items in a single variable.
We create a list using square brackets [].
students = ["Ravi", "Kumar", "Arun", "Vijay"]
print(students)
Output:
['Ravi', 'Kumar', 'Arun', 'Vijay']
Here, we stored four student names inside a single variable called students.
2. List Properties
Python lists have some important properties:
- Ordered: Items maintain their insertion order.
- Mutable: We can add, remove, or modify items.
- Allows duplicates: A list can contain duplicate values.
- Different data types: We can store different types of values in one list.
- Indexed: We can access items using their index.
Example:
data = [10, "Python", 20.5, True, 10]
print(data)
A list can contain integers, strings, floats, and Boolean values together.
3. Accessing List Elements
Every element in a list has an index.
Python uses zero-based indexing, which means the first element starts at index 0.
numbers = [10, 20, 30, 40, 50]
print(numbers[0]) # 10
print(numbers[1]) # 20
print(numbers[2]) # 30
We can also use negative indexing to access elements from the end.
print(numbers[-1]) # 50
print(numbers[-2]) # 40
4. List Slicing
Slicing allows us to access a portion of a list.
Syntax:
list[start : stop : step]
Example:
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Output:
[20, 30, 40]
Remember: The starting index is included, but the ending index is excluded.
We can also reverse a list using slicing:
print(numbers[::-1])
Output:
[50, 40, 30, 20, 10]
5. Modifying List Elements
Lists are mutable, so we can change their elements after creating them.
numbers = [10, 20, 30, 40]
numbers[1] = 100
print(numbers)
Output:
[10, 100, 30, 40]
Here, we changed the element at index 1 from 20 to 100.
6. Adding Elements to a List
Python provides different methods to add elements.
append()
Adds an element at the end of the list.
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
Output: [10, 20, 30, 40]
insert()
Adds an element at a specific position.
numbers.insert(1, 100)
print(numbers)
Output: [10, 100, 20, 30, 40]
extend()
Adds multiple elements from another iterable.
numbers = [10, 20]
numbers.extend([30, 40])
print(numbers)
Output: [10, 20, 30, 40]
7. Removing Elements from a List
We can remove elements using different methods.
remove() – Removes a specific value.
numbers = [10, 20, 30]
numbers.remove(20)
print(numbers)
pop() – Removes an element using its index and returns the removed value.
numbers = [10, 20, 30]
numbers.pop(1)
print(numbers)
del – Deletes an element or a portion of a list.
numbers = [10, 20, 30, 40]
del numbers[1]
print(numbers)
clear() – Removes all elements from the list.
numbers = [10, 20, 30]
numbers.clear()
print(numbers)
8. Useful List Methods
Here are some commonly used list methods:
| Method | Purpose |
|---|---|
append() |
Adds an item at the end |
insert() |
Adds an item at a specific position |
extend() |
Adds multiple items |
remove() |
Removes a specific value |
pop() |
Removes and returns an item |
clear() |
Removes all items |
index() |
Returns the index of an item |
count() |
Counts occurrences of an item |
sort() |
Sorts the list |
reverse() |
Reverses the list |
copy() |
Creates a shallow copy |
Example:
numbers = [40, 10, 30, 20]
numbers.sort()
print(numbers)
Output:
[10, 20, 30, 40]
9. Looping Through a List
We can use a for loop to access every element in a list.
students = ["Ravi", "Arun", "Vijay"]
for student in students:
print(student)
Output:
Ravi
Arun
Vijay
This is useful when we want to process every item in a list.
10. List Comprehension
List comprehension provides a shorter way to create a new list.
For example, let's create a list of even numbers.
Using a normal loop:
even_numbers = []
for num in range(10):
if num % 2 == 0:
even_numbers.append(num)
print(even_numbers)
Using list comprehension:
even_numbers = [num for num in range(10) if num % 2 == 0]
print(even_numbers)
Output:
[0, 2, 4, 6, 8]
Both approaches produce the same result, but list comprehension makes the code shorter.
Conclusion
Lists are one of the most important data structures in Python. They help us store, access, modify, and manage multiple values efficiently.
Top comments (0)