Lists in Python
In the previous lesson, we learned about Functions and how they help organize and reuse code efficiently. Today, we will learn about Lists, one of the most commonly used data structures in Python.
Lists are important because they allow programmers to store multiple values in a single variable.
What is a List?
A List is a collection of multiple items stored in a single variable.
Lists are:
- Ordered
- Changeable (Mutable)
- Allow duplicate values
Lists are created using square brackets "[]".
Example
languages = ["Python", "Java", "C++"]
Accessing List Items
Each item in a list has an index number.
Python indexing starts from "0".
Example
languages = ["Python", "Java", "C++"]
print(languages[0])
print(languages[1])
Output
Python
Java
Negative Indexing
Python allows accessing items from the end using negative indexes.
Example
languages = ["Python", "Java", "C++"]
print(languages[-1])
Output
C++
Changing List Items
Lists are mutable, meaning their values can be changed.
Example
languages = ["Python", "Java", "C++"]
languages[1] = "JavaScript"
print(languages)
Output
['Python', 'JavaScript', 'C++']
Adding Items to a List
Python provides several methods for adding items.
append()
The "append()" method adds an item to the end of the list.
Example
languages = ["Python", "Java"]
languages.append("C++")
print(languages)
Output
['Python', 'Java', 'C++']
insert()
The "insert()" method adds an item at a specific position.
Example
languages = ["Python", "Java"]
languages.insert(1, "C++")
print(languages)
Output
['Python', 'C++', 'Java']
Removing Items from a List
Python provides different methods to remove items.
remove()
The "remove()" method removes a specific item.
Example
languages = ["Python", "Java", "C++"]
languages.remove("Java")
print(languages)
Output
['Python', 'C++']
pop()
The "pop()" method removes an item using its index.
Example
languages = ["Python", "Java", "C++"]
languages.pop(1)
print(languages)
Output
['Python', 'C++']
Looping Through a List
Lists can be accessed using loops.
Example
languages = ["Python", "Java", "C++"]
for language in languages:
print(language)
Output
Python
Java
C++
List Length
The "len()" function returns the number of items in a list.
Example
languages = ["Python", "Java", "C++"]
print(len(languages))
Output
3
List Slicing
List Slicing allows accessing a range of items.
Example
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
Output
[20, 30, 40]
Sorting Lists
The "sort()" method arranges list items in ascending order.
Example
numbers = [5, 2, 8, 1]
numbers.sort()
print(numbers)
Output
[1, 2, 5, 8]
Reversing Lists
The "reverse()" method reverses the order of list items.
Example
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
Output
[4, 3, 2, 1]
Nested Lists
A Nested List is a list inside another list.
Example
students = [
["Augustine", 21],
["Brian", 22]
]
print(students[0])
Output
['Augustine', 21]
Why Lists are Important
Lists help programmers:
- Store multiple values
- Organize data
- Process collections of items
- Build dynamic applications
- Work with loops efficiently
Lists are widely used in real-world applications and databases.
Conclusion
Lists are one of the most useful and powerful data structures in Python programming. They allow storing, modifying, and managing collections of data efficiently.
Understanding Lists is important because they are heavily used in Python applications, data analysis, automation, and software development.
Practice creating and manipulating lists to strengthen your Python programming skills.
Top comments (0)