Python Advanced List Functions Cheat Sheet-1 with Examples
1. map()
Function
The map()
function applies a given function to all items in an iterable (like list) and returns a map object (which can be converted to a list).
Example: Square all numbers
numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x ** 2, numbers))
print(squared)
Output:
[1, 4, 9, 16]
✅ Also works with functions:
def double(x):
return x * 2
numbers = [1, 2, 3]
result = list(map(double, numbers))
print(result)
Output:
[2, 4, 6]
2. filter()
Function
The filter()
function filters elements from an iterable for which a function returns True
.
Example: Keep only even numbers
numbers = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)
Output:
[2, 4, 6]
✅ Also works with functions:
def is_positive(n):
return n > 0
nums = [-3, -2, 0, 4, 5]
pos = list(filter(is_positive, nums))
print(pos)
Output:
[4, 5]
3. reduce()
Function
The reduce()
function (from functools
) reduces a list to a single value by applying a function cumulatively to the items.
from functools import reduce
numbers = [1, 2, 3, 4]
result = reduce(lambda x, y: x + y, numbers)
print(result)
Output:
10
✅ Another Example: Multiply all elements
from functools import reduce
nums = [2, 3, 4]
product = reduce(lambda x, y: x * y, nums)
print(product)
Output:
24
⚠️ Note:
reduce()
is not built-in; import from functools
.
4. any()
Function
The any()
function returns True
if at least one element in the iterable is True
.
Example 1: With booleans
values = [False, False, True]
print(any(values))
Output:
True
Example 2: With numbers
nums = [0, 0, 5]
print(any(nums))
Output:
True
⚠️ Note:
0, None
, and False
are treated as False
values.
5. all()
Function
The all()
function returns True
only if all elements in the iterable are True
.
Example 1: With booleans
values = [True, True, True]
print(all(values))
Output:
True
Example 2: One False
in list
values = [True, False, True]
print(all(values))
Output:
False
✅ Point:
Useful in validations and conditions where all checks must pass.
6. zip()
Function
The zip()
function combines two or more iterables element-wise into tuples.
Example 1: Combine two lists
names = ["Alice", "Bob", "Charlie"]
scores = [85, 90, 88]
zipped = list(zip(names, scores))
print(zipped)
Output:
[('Alice', 85), ('Bob', 90), ('Charlie', 88)]
⚠️ Note:
If iterables are of different lengths, zip()
stops at the shortest one.
a = [1, 2, 3]
b = ['a', 'b']
print(list(zip(a, b)))
Output:
[(1, 'a'), (2, 'b')]
7. enumerate()
Function
The enumerate()
function adds a counter to an iterable and returns it as an enumerate object (tuple of index and value).
Example: Enumerate a list
fruits = ['apple', 'banana', 'mango']
for index, value in enumerate(fruits):
print(index, value)
Output:
0 apple
1 banana
2 mango
✅ You can also set a custom starting index:
for i, val in enumerate(fruits, start=1):
print(i, val)
Output:
1 apple
2 banana
3 mango
8. set()
Function
The set()
function creates a collection of unique elements (no duplicates).
Example: Remove duplicates from a list
numbers = [1, 2, 2, 3, 4, 4, 4]
unique = set(numbers)
print(unique)
Output:
{1, 2, 3, 4}
✅ Note:
set()
is unordered and cannot have duplicate values.
9. sorted()
Function
The sorted()
function returns a new sorted list from the items in an iterable (original list remains unchanged).
Example: Sort numbers
nums = [5, 2, 9, 1]
result = sorted(nums)
print(result)
Output:
[1, 2, 5, 9]
✅ Sort in descending order:
print(sorted(nums, reverse=True))
Output:
[9, 5, 2, 1]
✅ Sort strings by length:
words = ['banana', 'apple', 'kiwi']
print(sorted(words, key=len))
Output:
['kiwi', 'apple', 'banana']
10. reversed()
Function
The reversed()
function returns a reversed iterator of a sequence.
Example: Reverse a list
nums = [1, 2, 3, 4]
rev = list(reversed(nums))
print(rev)
Output:
[4, 3, 2, 1]
✅ Also works on strings:
text = "hello"
rev_text = ''.join(reversed(text))
print(rev_text)
Output:
olleh
📚 Practice Questions:
📚 Related Topics:
- ➤ Python Arithmetic Operators
- ➤ Basic String Functions
- ➤ Advanced String Functions
- ➤ Basic List Functions
- ➤ Advanced List Functions : Part-1
- ➤ Advanced List Functions : Part-2
- ➤ Basic Tuple Functions
- ➤ Advanced Tuple Functions
- ➤ Basic Dictionary Functions
- ➤ Advanced Dictionary Functions
- ➤ Conditional Statements : if-elif-else
- ➤ Python 'for' Loop
- ➤ Python 'while' Loop
- ➤ Difference between 'for' loop and 'while' loop
- ➤ Introducing Python Functions
📌 Bookmark this blog or follow for updates!
👍 Liked this post? Share it with friends or leave a comment below!