📚 List Data Type
Last Updated: 17th August 2025
- A list is an ordered collection of items stored in a single variable.
- Lists are mutable (you can change them) and allow duplicates.
- Lists use square brackets
[]. - All the indexing and slicing operations work on lists.
- Store multiple data types items in a single variable.
Hinglish Tip 🗣: List ko socho ek bucket jaise — items ek order me rakhe hote hain aur tum badal bhi sakte ho (add, remove, update).
✏ Creating Lists
empty = [] # or empty = list()
numbers = [10, 20, 30]
fruits = ["apple", "banana", "cherry"]
mixed = [1, "hello", 3.5, True]
print(type(empty))
🔎 Accessing Elements (Indexing)
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple
print(fruits[-1]) # cherry
🔁 Slicing
nums = [10,20,30,40,50]
print(nums[1:4]) # [20,30,40]
print(nums[:3]) # [10,20,30]
print(nums[::2]) # [10,30,50]
print(nums[::-1]) # reversed list
✍️ Modifying Lists
fruits = ["apple", "banana", "cherry"]
fruits[1] = "kiwi"
print(fruits) # ['apple', 'kiwi', 'cherry']
Loop over Lists
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# Use of index
for i in range(len(fruits)):
print(fruits[i])
Unpacking Lists
fruits = ["apple", "banana", "cherry"]
a, b, c = fruits
print(a) # apple
print(b) # banana
print(c) # cherry
💡 Quick Practice
- Create a list of your favorite fruits.
- Access the first and last fruits in the list.
- Modify the second fruit in the list.
- Loop through the list and print each fruit.
- Exercise