Skip to content

Commit 945f7c8

Browse files
authored
Implement utility functions for list and dict operations
Added functions to remove duplicates from a list, count occurrences of items in a list, and reverse a dictionary.
1 parent 752548b commit 945f7c8

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

‎Week03/sequences_esra_sepik.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def remove_duplicates(seq: list) -> list:
2+
"""
3+
This function removes duplicates from a list.
4+
"""
5+
return list(dict.fromkeys(seq))
6+
7+
8+
def list_counts(seq: list) -> dict:
9+
"""
10+
This function counts the number of occurrences of each item in a list.
11+
"""
12+
return {item: seq.count(item) for item in seq}
13+
14+
15+
def reverse_dict(d: dict) -> dict:
16+
"""
17+
This function reverses the keys and values of a dictionary.
18+
"""
19+
reversed_d = {}
20+
for key, value in d.items():
21+
reversed_d[value] = key
22+
return reversed_d

0 commit comments

Comments
 (0)