Skip to content

Instantly share code, notes, and snippets.

@raihan-uddin
Last active June 26, 2021 17:18
Show Gist options
  • Save raihan-uddin/fa6821e2f263e96c89fc2f106055e950 to your computer and use it in GitHub Desktop.
Save raihan-uddin/fa6821e2f263e96c89fc2f106055e950 to your computer and use it in GitHub Desktop.
Problem 1: count elements
input:
list = [50, 20, 11, 5, 7, 50, 7]
output:
dict {5: 1, 7 : 2, 11: 1, 20: 1, 50: 2}
Problem 2:
n_list = [5,6,8,10, 9,12]
op_list = [6,8,10,12]
Problem 3:
dict {3: 2, 5: 7, 2: 2, 7: 5, 9: 7}
op_dict {2: [2,3], 5: [7], 7: [7,9]]}
from collections import Counter
l = [50, 20, 11, 5, 7, 50, 7]
new_list = Counter(l)
print(new_list)
# dir(Counter)
# Problem 2
n_list = [5, 6, 8, 10, 9, 12]
even_list = []
for item in n_list:
if item % 2 == 0:
even_list.append(item)
# print(item)
print(even_list)
#Using list comprehension
even_no_list = [num for num in n_list if num % 2 == 0]
print(even_no_list)
#Using lambda expressions :
even_no_list2 = list(filter(lambda x: (x % 2 == 0), n_list))
print(even_no_list2)
# problem 3
dic = {3: 2, 5: 7, 2: 2, 7: 5, 9: 7}
new_dic = {}
for key, value in dic.items():
if value in new_dic:
new_dic[value].append(key)
else:
new_dic[value] = [key]
print(new_dic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment