Skip to content

Commit 0bef1dc

Browse files
authored
Merge pull request #668 from Yusuf003007/patch-3
Add functions to remove duplicates, count, and reverse dict
2 parents bdfcfb4 + d1e96d2 commit 0bef1dc

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

‎Week03/sequences_yusuf_baris.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
def remove_duplicates(seq: list) -> list:
2+
"""
3+
Remove duplicates from a list while preserving the order of elements.
4+
5+
Args:
6+
seq (list): The input list from which duplicates need to be removed.
7+
8+
Returns:
9+
list: A new list containing only unique elements from the input list.
10+
"""
11+
seen = set()
12+
result = []
13+
for item in seq:
14+
if item not in seen:
15+
seen.add(item)
16+
result.append(item)
17+
return result
18+
19+
20+
def list_counts(seq: list) -> dict:
21+
"""
22+
Count the occurrences of each element in a list.
23+
24+
Args:
25+
seq (list): The input list for which counts need to be calculated.
26+
27+
Returns:
28+
dict: A dictionary where keys are unique elements from the list and values are their respective counts.
29+
"""
30+
counts = {}
31+
for item in seq:
32+
if item in counts:
33+
counts[item] += 1
34+
else:
35+
counts[item] = 1
36+
return counts
37+
38+
39+
def reverse_dict(d: dict) -> dict:
40+
"""
41+
Reverse the keys and values of a dictionary.
42+
43+
Args:
44+
d (dict): The input dictionary to be reversed.
45+
46+
Returns:
47+
dict: A new dictionary with keys and values swapped.
48+
"""
49+
return {v: k for k, v in d.items()}

0 commit comments

Comments
 (0)