Working with Python Lists
What is a List in Python?
A list in Python is one of the most commonly used data structures. Think of it as a collection of items that can be stored in a specific order.
Key Characteristics
Ordered — Items have a specific order, so each item has an index starting from 0. The last element always have index -1.
Mutable —Unlike strings, Lists are mutable. You can change, add, or remove items after the list is created
Heterogeneous — A list can contain different data types: numbers, strings, booleans, other lists, etc.
Dynamic — You can grow or shrink the list as needed.
Creating Lists
# Empty list
empty_list = []
# List of numbers
numbers = [10, 20, 30, 40]
# List of strings
fruits = ["apple", "banana", "cherry"]
# Mixed data types
mixed = [1, "two", 3.5, True]
# List of lists
nested = [[1,2], [3,4], [5,6]]Accessing Elements
Elements can be accessed via Index. As mentioned first element always have index 0 and the last element have index -1.
fruits = ["apple", "banana", "cherry"]
fruits[0] # "apple" → first element
fruits[-1] # "cherry" → last element
fruits[0:2] # ["apple", "banana"] → slicing
fruits[4] # Error -> cannot access element Why Lists Are Useful
Lists can Store collections of related data
Iterate easily using loops
Perform mathematical or logical operations
Get Sameenah Tm’s stories in your inbox
Join Medium for free to get updates from this writer.
Can be nested (list of lists) for tables or matrices.
# A shopping list
shopping = ["milk", "bread", "eggs"]
# Add an item
shopping.append("butter")
# Remove an item
shopping.remove("bread")
# Loop through items
for item in shopping:
print(item)
# Output:
# milk
# eggs
# butter
"milk" in shopping #True
# Find all the methods in List
dir(list)
#Clear the list
shopping.clear()
#extend
shopping.extend(['jam', 'oats',])
#Count
numbers=[1,2,3,3,4,4,5,6,6,7,8,8]
print(numbers.count(4))
#2
# copy
new_numbers=numbers.copy()
print(new_numbers)
Adding Items
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Add at end
fruits.insert(1, "kiwi") # Add at index 1
fruits.extend(["mango","grape"]) # Add multipleRemoving Items
fruits.remove("banana") # Remove first occurrence
fruits.pop() # Remove last
fruits.pop(1) # Remove index 1
fruits.clear() # Remove all itemsSearching & Counting
fruits.index("cherry") # Get index
fruits.count("apple") # Count occurrencesCopying
copy_fruits = fruits.copy() # Shallow copy
same_list = fruits # References same listCommon Operations / Functions
len(fruits) # Number of items
sum(numbers) # Sum of numbers
min(numbers) # Minimum
max(numbers) # MaximumList Comprehensions (Very Useful)
squares = [x**2 for x in range(5)] # [0,1,4,9,16]
even = [x for x in range(10) if x % 2 == 0] # [0,2,4,6,8]Iterating Over Lists
for fruit in fruits:
print(fruit)
for i, fruit in enumerate(fruits):
print(i, fruit)Combining & Repeating
a = [1,2]
b = [3,4]
c = a + b # [1,2,3,4]
d = a * 3 # [1,2,1,2,1,2]Slicing Shortcuts
numbers[::-1] # Reverse list
numbers[::2] # Every second item
numbers[-3:] # Last 3 items
numbers[:-3] # All except last 3Quick Reference Table for List Methods
Method | Description |
-------------------- | -------------------------------------------------- |
`append(x)` | Add `x` at the end |
`insert(i,x)` | Insert `x` at index `i` |
`extend(iterable)` | Add all items from another iterable |
`remove(x)` | Remove first occurrence of `x` |
`pop([i])` | Remove and return item at index `i` (default last) |
`clear()` | Remove all items |
`index(x)` | Return index of first occurrence |
`count(x)` | Count occurrences of `x` |
`sort()` | Sort ascending |
`sort(reverse=True)` | Sort descending |
`reverse()` | Reverse list in place |
`copy()` | Return a shallow copy |


