Day 12: Python Programming
Published: (December 2, 2025 at 08:11 AM EST)
3 min read
Source: Dev.to
Source: Dev.to
PART-1: LIST – Advanced Concepts
List Comprehension
- With condition
Nested List Comprehension
List Slicing
Important List Methods
Example:
# Example of using list methods
my_list = [1, 2, 3, 4, 5]
my_list.append(6) # Add an element
my_list.extend([7, 8]) # Extend with another list
my_list.insert(0, 0) # Insert at the beginning
my_list.remove(3) # Remove first occurrence of 3
my_list.pop() # Remove and return the last item
my_list.clear() # Empty the list
Cloning a List
original = [1, 2, 3]
clone = original[:] # Shallow copy using slicing
# or
clone = list(original) # Using the list constructor
PART-2: TUPLE – Advanced Concepts
Tuple Unpacking
point = (10, 20)
x, y = point
Tuple with * (Extended Unpacking)
numbers = (1, 2, 3, 4, 5)
first, *middle, last = numbers
Why Tuple Is Faster?
- Immutable → stored in continuous memory
- Faster iteration than list
- Ideal for fixed data (e.g., latitude/longitude, configuration)
Convert List to Tuple
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
PART-3: SET – Advanced Concepts
Set Operations
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # union -> {1, 2, 3, 4, 5}
print(A & B) # intersection -> {3}
print(A - B) # difference -> {1, 2}
Remove Duplicates from List Using Set
my_list = [1, 2, 2, 3, 4, 4]
unique = list(set(my_list)) # [1, 2, 3, 4] (order not guaranteed)
Add & Remove Elements
s = {1, 2, 3}
s.add(4) # {1, 2, 3, 4}
s.discard(2) # {1, 3, 4}
s.remove(3) # {1, 4}
Set Comprehension
squared = {x**2 for x in range(5)} # {0, 1, 4, 9, 16}
PART-4: DICTIONARY – Advanced Concepts
Dictionary Comprehension
squares = {x: x*x for x in range(5)} # {0:0, 1:1, 2:4, 3:9, 4:16}
Looping Through Dictionary
d = {'a': 1, 'b': 2}
for key, value in d.items():
print(key, value)
Merging Two Dictionaries
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
merged = {**d1, **d2} # {'a': 1, 'b': 3, 'c': 4}
# Python 3.9+ alternative:
merged = d1 | d2
Dictionary Methods
dict.get(key, default)dict.keys(),dict.values(),dict.items()dict.update(other_dict)dict.pop(key),dict.popitem()dict.clear()
Using Dictionary as Counter
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
counter = {}
for w in words:
counter[w] = counter.get(w, 0) + 1
# counter -> {'apple': 3, 'banana': 2, 'orange': 1}
PART-5: Collections Module (Important for Interviews)
Counter
from collections import Counter
cnt = Counter(['a', 'b', 'a', 'c', 'b', 'a'])
# Counter({'a': 3, 'b': 2, 'c': 1})
defaultdict
from collections import defaultdict
dd = defaultdict(list)
dd['key'].append(1) # automatically creates a list for 'key'
deque
from collections import deque
dq = deque([1, 2, 3])
dq.appendleft(0) # deque([0, 1, 2, 3])
dq.pop() # 3
OrderedDict
from collections import OrderedDict
od = OrderedDict()
od['first'] = 1
od['second'] = 2
# Preserves insertion order (Python 3.7+ dict does this by default)
PART-6: Interview Programs Using Collections
Find the Most Repeated Element
from collections import Counter
def most_common(lst):
return Counter(lst).most_common(1)[0][0]
print(most_common([1, 2, 2, 3, 3, 3])) # 3
Reverse a List Without Using reverse()
lst = [1, 2, 3, 4]
reversed_lst = lst[::-1] # [4, 3, 2, 1]
Merge Two Lists into a Dictionary
keys = ['a', 'b', 'c']
values = [1, 2, 3]
merged_dict = dict(zip(keys, values)) # {'a': 1, 'b': 2, 'c': 3}
Remove Duplicates While Preserving Order
def dedupe(seq):
seen = set()
return [x for x in seq if not (x in seen or seen.add(x))]
print(dedupe([3, 1, 2, 3, 2, 1])) # [3, 1, 2]
Convert Dictionary to List of Tuples
d = {"a": 1, "b": 2}
print(list(d.items())) # [('a', 1), ('b', 2)]