Python List and List Comprehension
What is a List in Python?
A list in Python is a mutable, ordered collection of items, allowing duplicate elements. Lists can hold any data type — strings, integers, floats, other lists, etc.
Syntax:
my_list = [item1, item2, item3, ...]
Example: Create and Print a List
fruits = ["apple", "banana", "cherry"]
print(fruits)
Output:
['apple', 'banana', 'cherry']
Python List Operations
Operation | Example | Result |
---|---|---|
Access item | fruits[0] | 'apple' |
Slice | fruits[1:] | ['banana', 'cherry'] |
Append item | fruits.append("mango") | ['apple', 'banana', 'cherry', 'mango'] |
Insert item | fruits.insert(1, "kiwi") | ['apple', 'kiwi', 'banana', 'cherry'] |
Remove item | fruits.remove("banana") | ['apple', 'cherry'] |
Pop item | fruits.pop() | Removes last element |
Length of list | len(fruits) | 3 |
Check existence | "apple" in fruits | True |
Loop Through a List
for fruit in fruits:
print(fruit)
Output:
apple banana cherry
Python List Comprehension
What is List Comprehension?
List comprehension provides a shorter, more readable syntax to create new lists from existing iterables.
Syntax:
new_list = [expression for item in iterable if condition]
Example 1: Basic List Comprehension
numbers = [1, 2, 3, 4, 5]
squares = [n ** 2 for n in numbers]
print(squares)
Output:
[1, 4, 9, 16, 25]
Example 2: With Condition
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)
Output:
[2, 4]
Example 3: Using range() and List Comprehension
multiples_of_3 = [n for n in range(1, 21) if n % 3 == 0]
print(multiples_of_3)
Output:
[3, 6, 9, 12, 15, 18]
Example 4: Nested List Comprehension
matrix = [[row * col for col in range(1, 4)] for row in range(1, 4)]
print(matrix)
Output:
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]
Real-World Example: Filter Words by Length
words = ["python", "is", "awesome", "fun"]
long_words = [word for word in words if len(word) > 3]
print(long_words)
Output:
['python', 'awesome']
Python List Methods with Examples
1. append()
Adds an element to the end of the list.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)
Output:
['apple', 'banana', 'cherry']
2. insert(index, element)
Inserts an element at a specific index.
fruits.insert(1, "mango")
print(fruits)
Output:
['apple', 'mango', 'banana', 'cherry']
3. extend(iterable)
Adds all elements from another iterable (list, tuple, etc.).
fruits.extend(["orange", "grape"])
print(fruits)
Output:
['apple', 'mango', 'banana', 'cherry', 'orange', 'grape']
4. remove(value)
Removes the first occurrence of a value.
fruits.remove("banana")
print(fruits)
Output:
['apple', 'mango', 'cherry', 'orange', 'grape']
5. pop(index=-1)
Removes and returns the item at the given index (default is last).
fruits.pop()
print(fruits)
Output:
['apple', 'mango', 'cherry', 'orange']
6. clear()
Removes all elements from the list.
fruits.clear()
print(fruits)
Output:
[]
7. index(value)
Returns the index of the first matching element.
numbers = [10, 20, 30, 40]
print(numbers.index(30))
Output:
2
8. count(value)
Counts the number of times a value occurs.
marks = [90, 85, 90, 75]
print(marks.count(90))
Output:
2
9. sort(reverse=False)
Sorts the list in ascending (default) or descending order.
nums = [3, 1, 4, 2]
nums.sort()
print(nums)
Output:
[1, 2, 3, 4]
Descending sort:
nums.sort(reverse=True)
print(nums)
Output:
[4, 3, 2, 1]
10. reverse()
Reverses the order of elements in the list.
nums = [1, 2, 3]
nums.reverse()
print(nums)
Output:
[3, 2, 1]
11. copy()
Returns a shallow copy of the list.
original = [1, 2, 3]
copy_list = original.copy()
print(copy_list)
Output:
[1, 2, 3]
Bonus: len() and Membership Operators
Although not list methods, these are commonly used with lists:
len()
print(len([1, 2, 3]))
Output:
3
in / not in
print(2 in [1, 2, 3])
print(5 not in [1, 2, 3])
Output:
True True
Summary Table
Method | Description |
---|---|
append() | Add single item to the end |
insert() | Insert item at specific position |
extend() | Add elements from another iterable |
remove() | Remove first match of an item |
pop() | Remove and return item by index |
clear() | Remove all elements |
index() | Return index of first occurrence |
count() | Count how many times a value appears |
sort() | Sort the list |
reverse() | Reverse the list |
copy() | Return shallow copy |