📚 Python List — The Most Versatile Data Structure
Introduction 🌟
A list in Python is an ordered, mutable (changeable) collection of items. Lists can store **any data type** — integers, floats, strings, booleans, or even other lists. They are one of the most powerful and commonly used data structures in Python.
Note
💡 Lists are defined using [ ] square brackets.
1. Creating Lists 🧱
create_list.py
# Empty list
my_list = []
# List with values
numbers = [10, 20, 30]
# Mixed data types
mixed = [10, "hello", 3.5, True]
# Nested list
nested = [1, 2, [3, 4, 5]]2. Accessing List Elements 🔢
access_elements.py
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # mango (last element)Note
✔️ Indexing starts from 0.
✔️ Negative indexing starts from the end.
✔️ Negative indexing starts from the end.
3. Slicing Lists ✂️
list_slicing.py
nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
print(nums[:3]) # [10, 20, 30]
print(nums[2:]) # [30, 40, 50]
print(nums[::-1]) # reverse list4. Modifying Lists ✏️
modify_list.py
fruits = ["apple", "banana", "mango"]
fruits[1] = "orange"
print(fruits) # ["apple", "orange", "mango"]5. Adding Elements ➕
append() — Add at end
append.py
fruits = ["apple", "banana"]
fruits.append("mango")
print(fruits)insert() — Add at specific position
insert.py
fruits.insert(1, "orange")
print(fruits)extend() — Add multiple items
extend.py
fruits.extend(["grape", "kiwi"])
print(fruits)6. Removing Elements ➖
remove()
remove.py
fruits.remove("banana")pop()
pop.py
fruits.pop() # removes last
fruits.pop(1) # removes at index 1clear()
clear.py
fruits.clear()7. Searching in Lists 🔍
search_list.py
nums = [10, 20, 30]
print(20 in nums) # True
print(nums.index(30)) # 28. Sorting and Reversing 🔄
sort_reverse.py
nums = [40, 10, 30, 20]
nums.sort() # ascending
nums.sort(reverse=True) # descending
nums.reverse() # reverse order9. List Methods Summary 🧰
| Method | Description |
|---|---|
| append() | Adds item at end |
| insert() | Adds at given index |
| extend() | Adds multiple items |
| remove() | Removes first matching value |
| pop() | Removes by index |
| clear() | Empties the list |
| index() | Returns index of value |
| count() | Counts occurrences |
| sort() | Sorts list |
| reverse() | Reverses order |
10. Nested Lists 🧩
nested_list.py
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(matrix[1][2]) # 611. List Comprehensions ⚡
A short and powerful way to create lists.
list_comprehension.py
squares = [x*x for x in range(1, 6)]
print(squares)Note
💡 List comprehensions replace long loops with cleaner expressions.
12. Real-World Example 🌍
real_world_example.py
emails = ["a@gmail.com", "b@gmail.com", "invalid", "c@gmail.com"]
valid = []
for email in emails:
if "@" in email:
valid.append(email)
print("Valid emails:", valid)Conclusion 🎉
>>“Lists are the backbone of Python — flexible, powerful, and essential for everyday programming.” ✨
You now understand Python Lists completely! Want the next topic? Try Tuple, Set, Dictionary, List Comprehension, or Functions. Just tell me! 😊