List Operations
Concatenation, repetition, membership
Interview Relevant: Basic operations
List Operations
Common list operations and patterns.
Code Examples
List operations and built-in functions.
python
1# Concatenation
2[1, 2] + [3, 4] # [1, 2, 3, 4]
3
4# Repetition
5[0] * 5 # [0, 0, 0, 0, 0]
6
7# Membership
83 in [1, 2, 3] # True
9
10# Comparison
11[1, 2] == [1, 2] # True
12[1, 2] < [1, 3] # True (lexicographic)
13
14# sorted() - returns new list
15sorted([3, 1, 2]) # [1, 2, 3]
16sorted([3, 1, 2], reverse=True) # [3, 2, 1]
17sorted(["b", "a"], key=str.lower)
18
19# reversed() - returns iterator
20list(reversed([1, 2, 3])) # [3, 2, 1]
21
22# zip - combine lists
23list(zip([1, 2], ['a', 'b'])) # [(1, 'a'), (2, 'b')]