0% found this document useful (0 votes)
9 views3 pages

Python List Operations Cheatsheet

This document is a cheat sheet for Python list operations, covering basic operations, iteration patterns, list comprehensions, and advanced functions. It includes examples for each operation, such as concatenation, slicing, filtering, and mapping. Additionally, it provides real-life examples demonstrating how to flatten lists, filter and transform data, and check conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Python List Operations Cheatsheet

This document is a cheat sheet for Python list operations, covering basic operations, iteration patterns, list comprehensions, and advanced functions. It includes examples for each operation, such as concatenation, slicing, filtering, and mapping. Additionally, it provides real-life examples demonstrating how to flatten lists, filter and transform data, and check conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python List Operations Cheat Sheet

1️⃣ Basic Operations

Operation / Function What it does Example

len(list) Returns number of elements len([1,2,3]) → 3

Indexing Access an element by position lst[0] → 1

Negative indexing Access from end lst[-1] → last element

Slicing Get a sublist [start:end:step] lst[1:4] → elements 1,2,3

Concatenation (+) Merge two lists into a new one [1,2] + [3,4] → [1,2,3,4]

Repetition (*) Repeat list elements [0]*3 → [0,0,0]

in / not in Membership check 3 in [1,2,3] → True

min(), max() Find smallest / largest max([1,5,3]) → 5

sum() Sum numeric elements sum([1,2,3]) → 6

2️⃣ Iteration Patterns

Pattern What it does Example

for loop Traverse elements for x in lst: print(x)

enumerate() Get index + element for i, x in enumerate(lst): ...

zip() Combine multiple iterables for a,b in zip([1,2],[3,4]): ...

reversed() Iterate in reverse order for x in reversed(lst): ...

sorted() Iterate over sorted copy for x in sorted(lst): ...

3️⃣ List Comprehensions & Expressions

• Basic: [x*2 for x in lst] → [2,4,6]


• With condition: [x for x in lst if x%2==0] → only even numbers
• Nested: [y for x in matrix for y in x] → flatten a 2D list
• With function: [func(x) for x in lst] → apply a function to all elements

4️⃣ Advanced / Deeper Level Operations

1
Operation /
Purpose Example
Function

Check conditions across all / any


all() / any() all(x>0 for x in lst)
elements

map(func, iterable) Apply a function to all items list(map(str, [1,2])) → ['1','2']

Keep elements that satisfy


filter(func, iterable) list(filter(lambda x: x%2==0, lst))
condition

reduce(func,
Reduce list to single value from functools import reduce
iterable)

Combine multiple iterables


itertools.chain() list(chain([1,2],[3,4]))
efficiently

list(product([1,2],[3,4])) → [(1,3),(1,4),
itertools.product() Cartesian product
(2,3),(2,4)]

list slicing with step Select every nth element lst[::2]

copy() / deepcopy() Make independent copies of lists See previous explanation

del lst[i:j] Remove slice of elements del lst[1:3]

⚙️ Real-Life Examples

Flatten nested list:

matrix = [[1,2],[3,4]]
flat = [y for x in matrix for y in x] # [1,2,3,4]

Filter and transform:

numbers = [1,2,3,4,5]
squares_of_even = [x**2 for x in numbers if x%2==0] # [4,16]

Check all conditions:

grades = [90, 85, 78]


all_pass = all(g>=50 for g in grades) # True

Iterate with index and value:

2
names = ["Alice", "Bob"]
for i, name in enumerate(names):
print(i, name)

Merge multiple sources:

a = [1,2]; b=[3,4]
combined = list(itertools.chain(a,b)) # [1,2,3,4]

You might also like