Skip to content

Commit c01736b

Browse files
authored
Implement utility functions for list and dict operations
Added functions to remove duplicates from a list, count element occurrences, and reverse a dictionary.
1 parent b536459 commit c01736b

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎Week03/sequences_goktug_kayaci.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def remove_duplicates(seq: list) -> list:
2+
"""
3+
Remove repeated elements from a list.
4+
"""
5+
unique_items = []
6+
7+
for value in seq:
8+
if value not in unique_items:
9+
unique_items.append(value)
10+
11+
return unique_items
12+
13+
14+
def list_counts(seq: list) -> dict:
15+
"""
16+
Count how many times each element appears in a list.
17+
"""
18+
result = {}
19+
20+
for value in seq:
21+
if value in result:
22+
result[value] += 1
23+
else:
24+
result[value] = 1
25+
26+
return result
27+
28+
29+
def reverse_dict(d: dict) -> dict:
30+
"""
31+
Exchange the keys and values of a dictionary.
32+
"""
33+
reversed_dict = {}
34+
35+
for key, value in d.items():
36+
reversed_dict[value] = key
37+
38+
return reversed_dict

0 commit comments

Comments
 (0)