A list is a container that holds multiple items in a single variable, in a specific order.
How to Create a Lists:
# Empty list
my_list = []
# List of numbers
numbers = [1, 2, 3, 4, 5]
# List of strings
names = ["Alice", "Bob", "Charlie"]
# Mixed list (different data types)
mixed = [1, "hello", 3.14, True]
# List inside a list (nested)
nested = [[1, 2], [3, 4], [5, 6]]
Accessing Items — Indexing
fruits = ["apple", "banana", "mango", "orange"]
#index 0 1 2 3
print(fruits[0]) # apple ← first item
print(fruits[2]) # mango ← third item
print(fruits[-1]) # orange ← last item (negative index)
print(fruits[-2]) # mango ← second from last
Slicing a List
fruits = ["apple", "banana", "mango", "orange", "grape"]
print(fruits[1:3]) # ["banana", "mango"] ← index 1 to 2
print(fruits[:3]) # ["apple","banana","mango"] ← start to index 2
print(fruits[2:]) # ["mango","orange","grape"] ← index 2 to end
print(fruits[::2]) # ["apple","mango","grape"] ← every 2nd item
Most Used List Methods
fruits = ["apple", "banana", "mango"]
# ADD items
fruits.append("orange") # adds to END → ["apple","banana","mango","orange"]
fruits.insert(1, "grape") # adds at index 1 → ["apple","grape","banana"...]
# REMOVE items
fruits.remove("banana") # removes by VALUE
fruits.pop() # removes LAST item
fruits.pop(0) # removes item at index 0
# FIND items
print(len(fruits)) # total count of items
print(fruits.index("mango")) # finds index of "mango"
print("apple" in fruits) # True/False — does it exist?
# SORT items
fruits.sort() # sorts A→Z or small→big
fruits.sort(reverse=True) # sorts Z→A or big→small
fruits.reverse() # reverses the order
# COPY & CLEAR
copy = fruits.copy() # makes a copy of the list
fruits.clear() # empties the entire list
# ADD items
fruits.append("orange") # adds to END → ["apple","banana","mango","orange"]
fruits.insert(1, "grape") # adds at index 1 → ["apple","grape","banana"...]
# REMOVE items
fruits.remove("banana") # removes by VALUE
fruits.pop() # removes LAST item
fruits.pop(0) # removes item at index 0
# FIND items
print(len(fruits)) # total count of items
print(fruits.index("mango")) # finds index of "mango"
print("apple" in fruits) # True/False — does it exist?
# SORT items
fruits.sort() # sorts A→Z or small→big
fruits.sort(reverse=True) # sorts Z→A or big→small
fruits.reverse() # reverses the order
# COPY & CLEAR
copy = fruits.copy() # makes a copy of the list
fruits.clear() # empties the entire list
Looping Through a List
fruits = ["apple", "banana", "mango"]
# Basic loop
for fruit in fruits:
print(fruit)
# apple
# banana
# mango
fruits = ["apple", "banana", "mango"]
# Basic loop
for fruit in fruits:
print(fruit)
# apple
# banana
# mango
# Loop with index
for i, fruit in enumerate(fruits):
print(i, fruit)
# 0 apple
# 1 banana
# 2 mango
for i, fruit in enumerate(fruits):
print(i, fruit)
# 0 apple
# 1 banana
# 2 mango
# creating a simple list in python
groceries = ["Apple", "Banana", "Pineapple", "Cherry"]
print(groceries)
# Accessing elements
groceries = ["Apple", "Banana", "Pineapple", "Cherry"]
print(groceries)
# Accessing elements
groceries = ["milk", "noodles", "bread"]
print(groceries[2])
print(groceries[-1])
# Modifying a list:
my_list = ["apple", "banana", "cherry", "pineapple"]
print(my_list)
my_list[0] = "strawberry"
print(my_list)
# Adding elements:
print(groceries[2])
print(groceries[-1])
# Modifying a list:
my_list = ["apple", "banana", "cherry", "pineapple"]
print(my_list)
my_list[0] = "strawberry"
print(my_list)
# Adding elements:
my_list = ["apple", "banana", "cherry"]
my_list.append("grapes") # add grapes
print(my_list)
# Insert elements:
my_list = ["apple", "banana", "cherry", "kiwi"]
print(my_list)
my_list.insert(1, "raspberry") # insert raspberry
print(my_list)
# Removing elements (using 3 methods)
my_list.append("grapes") # add grapes
print(my_list)
# Insert elements:
my_list = ["apple", "banana", "cherry", "kiwi"]
print(my_list)
my_list.insert(1, "raspberry") # insert raspberry
print(my_list)
# Removing elements (using 3 methods)
1)
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
my_list.remove ("apple") # remove apple
print(my_list)
2)
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
my_list.pop() # it will remove the last value "cherry"
print(my_list)
3)
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
my_list.remove ("apple") # remove apple
print(my_list)
2)
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
my_list.pop() # it will remove the last value "cherry"
print(my_list)
3)
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
del my_list[1]
print(my_list)
# Length and looping:
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
print(len(my_list)
for item in my_list
print(item)
# List slicing:
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
print(my_list[1:3])
# Sorting and Reversing:
num = [2,4,6,3,0,11,19,22,35]
num.sort()
print(num)
rum.reverse()
print(num)
print(my_list)
del my_list[1]
print(my_list)
# Length and looping:
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
print(len(my_list)
for item in my_list
print(item)
# List slicing:
my_list = ["apple", "banana", "berry", "cherry"]
print(my_list)
print(my_list[1:3])
# Sorting and Reversing:
num = [2,4,6,3,0,11,19,22,35]
num.sort()
print(num)
rum.reverse()
print(num)