Lists in Python
Lists are one of the most powerful and commonly used data structures in Python. A list allows you to store multiple values in a single variable. Lists are flexible, dynamic, and can hold different types of data together.
In real-world applications, lists are used everywhere — storing user data, processing records, holding database rows, handling API responses, or organizing items in an application.
What Is a List?
A list is an ordered collection of items enclosed in square brackets [ ].
Each item has an index starting from 0.
Lists can store numbers, strings, floats, booleans, or even other lists.
my_list = [10, "dataplexa", 3.14, True]
This list contains different types of values — Python allows this flexibility.
Why Lists Are Important?
Lists make your programs efficient because you can store and process large amounts of related data easily. They are widely used because:
- They are dynamic (you can add/remove items anytime)
- They maintain order
- They support slicing and indexing
- They can hold different data types together
- They are extremely fast for read/write operations
Creating Lists
Lists can be created in different ways:
numbers = [1, 2, 3, 4]
names = ["ram", "shyam", "dataplexa"]
mixed = [10, "hello", 5.5, False]
empty = []
Accessing List Elements (Indexing)
You can access individual elements using their index positions. Indexing starts from 0.
items = ["apple", "banana", "mango"]
print(items[0]) # apple
print(items[2]) # mango
print(items[-1]) # last item: mango
List Slicing
Slicing helps you extract a part of a list. This is useful when working with large data sets.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
print(numbers[:3]) # [10, 20, 30]
print(numbers[2:]) # [30, 40, 50]
Modifying Lists
Lists are mutable, meaning you can change their values anytime.
items = ["apple", "banana", "mango"]
items[1] = "orange"
print(items) # ["apple", "orange", "mango"]
Adding Elements to a List
1. append() – Adds item at the end
items = [1, 2, 3]
items.append(4)
print(items)
2. insert() – Adds item at a specific index
items = ["a", "b", "c"]
items.insert(1, "new")
print(items)
3. extend() – Adds all items from another list
a = [1, 2]
b = [3, 4, 5]
a.extend(b)
print(a)
Removing Elements from a List
1. remove() – Removes the first matching value
items = [10, 20, 30, 20]
items.remove(20)
print(items)
2. pop() – Removes item by index
items = ["x", "y", "z"]
items.pop(1)
print(items)
3. clear() – Removes all items
items = [1, 2, 3]
items.clear()
print(items)
Useful List Methods
| Method | Description |
|---|---|
| append() | Add item to end |
| insert() | Add item at index |
| extend() | Add multiple items |
| remove() | Remove by value |
| pop() | Remove by index |
| sort() | Sort list ascending |
| reverse() | Reverse order |
Sorting Lists
Sorting is useful for organizing data alphabetically or numerically.
numbers = [40, 10, 20, 5]
numbers.sort()
print(numbers)
Looping Through a List
You can iterate through lists using a loop.
items = ["pen", "book", "laptop"]
for i in items:
print(i)
List Comprehension (Advanced Feature)
List comprehension is a shorter and more efficient way to create lists.
squares = [x*x for x in range(1, 6)]
print(squares)
Real-World Example: Shopping Cart System
cart = []
cart.append("Phone")
cart.append("Charger")
cart.append("Earbuds")
print("Your Cart Items:")
for item in cart:
print("-", item)
Real-World Example: Processing Student Scores
scores = [88, 92, 75, 91, 85]
avg = sum(scores) / len(scores)
print("Average Score:", avg)
📝 Practice Exercises
- Create a list of 5 fruits and print the second and last elements.
- Write a program to find the largest number in a list.
- Remove all even numbers from a list using a loop.
- Sort a list of names alphabetically.
- Using list comprehension, create a list of squares from 1 to 10.
✅ Practice Answers
Answer 1:
fruits = ["apple", "banana", "mango", "orange", "grapes"]
print(fruits[1], fruits[-1])
Answer 2:
numbers = [10, 20, 55, 2, 90]
print(max(numbers))
Answer 3:
nums = [1,2,3,4,5,6,7,8,9]
result = []
for n in nums:
if n % 2 != 0:
result.append(n)
print(result)
Answer 4:
names = ["siri", "ram", "anil", "john"]
names.sort()
print(names)
Answer 5:
squares = [x*x for x in range(1, 11)]
print(squares)