In Python, lists are one of the most powerful and commonly used data types. A list allows you to store multiple items in a single variable, even if the items are of different types.
Lists are widely used in:
- Data storage
- Web applications
- Data analysis
- Automation tasks
- Real-world programming projects
🔹 What is a List in Python?
A list is an ordered, changeable (mutable) collection of items.
🔹 Syntax:
my_list = [item1, item2, item3]
🔹 Creating Lists in Python
fruits = ["apple", "banana", "mango"]
print(fruits)
🔸 Output:
['apple', 'banana', 'mango']
🔹 List Can Store Different Data Types
data = ["Python", 100, 3.14, True]
print(data)
🔸 Output:
['Python', 100, 3.14, True]
🔹 Accessing List Elements (Indexing)
fruits = ["apple", "banana", "mango"]
print(fruits[0])
print(fruits[2])
🔸 Output:
apple
mango
🔹 Negative Indexing
fruits = ["apple", "banana", "mango"]
print(fruits[-1])
print(fruits[-2])
🔸 Output:
mango
banana
🔹 List Slicing
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
🔸 Output:
[20, 30, 40]
🔹 Changing List Items (Mutable)
fruits = ["apple", "banana", "mango"]
fruits[1] = "orange"
print(fruits)
🔸 Output:
['apple', 'orange', 'mango']
🔹 Adding Items to a List
🔹 append() → Add at end
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)
🔹 insert() → Add at specific position
fruits = ["apple", "mango"]
fruits.insert(1, "banana")
print(fruits)
🔹 extend() → Add multiple items
fruits = ["apple"]
fruits.extend(["banana", "mango"])
print(fruits)
🔹 Removing Items from List
🔹 remove() → Remove specific item
fruits = ["apple", "banana", "mango"]
fruits.remove("banana")
print(fruits)
🔹 pop() → Remove by index
fruits = ["apple", "banana", "mango"]
fruits.pop(1)
print(fruits)
🔹 clear() → Remove all items
fruits = ["apple", "banana"]
fruits.clear()
print(fruits)
🔸 Output:
[]
🔹 Loop Through a List
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
🔸 Output:
apple
banana
mango
🔹 List Length
fruits = ["apple", "banana", "mango"]
print(len(fruits))
🔸 Output:
3
🔹 Check Item in List
fruits = ["apple", "banana", "mango"]
if "banana" in fruits:
print("Found")
🔸 Output:
Found
🔹 Sorting a List
numbers = [5, 2, 9, 1]
numbers.sort()
print(numbers)
🔸 Output:
[1, 2, 5, 9]
🔹 Reverse a List
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
🔸 Output:
[4, 3, 2, 1]
🔹 Copy a List
list1 = [1, 2, 3]
list2 = list1.copy()
print(list2)
🔹 Join Two Lists
a = [1, 2]
b = [3, 4]
print(a + b)
🔸 Output:
[1, 2, 3, 4]
🔹 Nested Lists
matrix = [
[1, 2],
[3, 4]
]
print(matrix[0][1])
🔸 Output:
2
🔹 Real-Life Example: Student Marks
marks = [80, 90, 70, 85]
print("Total:", sum(marks))
print("Average:", sum(marks) / len(marks))
🔹 Real-Life Example: Shopping Cart
cart = ["milk", "bread", "eggs"]
cart.append("butter")
print(cart)
🔹 Real-Life Example: Top Scores
scores = [50, 90, 75, 100, 85]
scores.sort(reverse=True)
print(scores)
🔸 Output:
[100, 90, 85, 75, 50]
🔹 Important Properties of Lists
✔ Ordered collection
✔ Mutable (can be changed)
✔ Allows duplicates
✔ Supports multiple data types
✔ Supports indexing and slicing
🔹 Common List Methods
| Method | Description |
|---|---|
| append() | Add item at end |
| insert() | Add item at position |
| remove() | Remove item |
| pop() | Remove by index |
| sort() | Sort list |
| reverse() | Reverse list |
| copy() | Copy list |
| clear() | Remove all items |
🔹 Why Lists are Important?
✔ Store multiple values
✔ Easy data manipulation
✔ Used in loops and algorithms
✔ Essential for real-world apps
✔ Foundation for data structures
🚀 Conclusion
Python lists are one of the most powerful and flexible data structures. They allow you to store, manage, and manipulate multiple values easily.
Once you master lists, you can:
- Build real applications
- Handle large datasets
- Solve coding problems
- Work with AI and data science


0 Comments