Advanced Python Collections (List, Tuple, Set, Dictionary)
PART-1: LIST – Advanced Concepts
1. List Comprehension
Short way to create lists.
squares = [x*x for x in range(1, 6)]
print(squares)
With condition:
even = [x for x in range(10) if x % 2 == 0]
2. Nested List Comprehension
matrix = [[i*j for j in range(1, 4)] for i in range(1, 4)]
3.List Slicing
nums = [1,2,3,4,5,6]
print(nums[1:5])
print(nums[::-1]) # reverse
4.Important List Methods
append()
extend()
insert()
remove()
pop()
clear()
sort()
reverse()
count()
index()
Example:
items = [5, 2, 9, 1]
items.sort() # ascending
items.sort(reverse=True) # descending
5.Cloning a List
a = [1, 2, 3]
b = a[:]
c = a.copy()
PART-2: TUPLE – Advanced Concepts
1.Tuple Unpacking
a, b, c = (10, 20, 30)
2.Tuple with * (Extended Unpacking)
a, *b = (1, 2, 3, 4, 5)
3.Why Tuple Is Faster?
- Immutable → stored in continuous memory
- Faster iteration than list
- Used for fixed data (lat/long, config)
4.Convert List to Tuple
t = tuple([1, 2, 3])
PART-3: SET – Advanced Concepts
1.Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # union
print(A & B) # intersection
print(A - B) # difference
✔2.Remove Duplicates from List Using Set
lst = [1,2,2,3,3,4]
unique = list(set(lst))
3.Add & Remove Elements
s = {1,2,3}
s.add(4)
s.discard(2)
s.remove(3) # error if 3 not exists
4.Set Comprehension
s = {x*x for x in range(5)}
PART-4: DICTIONARY – Advanced Concepts
1.Dictionary Comprehension
squares = {x: x*x for x in range(5)}
2.Looping Through Dictionary
d = {"a":1, "b":2, "c":3}
for key in d:
print(key, d[key])
for k, v in d.items():
print(k, v)
3.Merging Two Dictionaries
d1 = {"a": 1, "b": 2}
d2 = {"c": 3}
merged = {**d1, **d2}
4.Dictionary Methods
get()
items()
keys()
values()
pop()
popitem()
update()
clear()
5.Using Dictionary as Counter
data = "aabbccc"
count = {}
for ch in data:
count[ch] = count.get(ch, 0) + 1
print(count)
PART-5: Collections Module (Important for Interviews)
from collections import Counter, defaultdict, deque, OrderedDict
1.Counter
Counts frequency of elements.
from collections import Counter
print(Counter("aabbbcccc"))
2.defaultdict
Automatically creates default values if key missing.
from collections import defaultdict
d = defaultdict(int)
d["a"] += 1
d["b"] += 2
print(d)
3.deque
Fast append/pop from both ends.
from collections import deque
q = deque([1,2,3])
q.appendleft(0)
q.append(4)
print(q)
4.OrderedDict
Remembers insertion order
(Python 3.7+ dict already preserves order)
PART-6: Interview Programs Using Collections
1.Find the most repeated element
from collections import Counter
print(Counter([1,2,2,3,3,3]).most_common(1))
2.Reverse a list without using reverse()
lst = [1,2,3]
print(lst[::-1])
3.Merge two lists into dictionary
keys = ["name", "age"]
values = ["Arun", 22]
d = dict(zip(keys, values))
4.Remove duplicates while preserving order
result = []
for x in [1,2,2,3,1]:
if x not in result:
result.append(x)
5.Convert dictionary to list of tuples
d = {"a":1, "b":2}
print(list(d.items()))
Top comments (0)