Skip to content

Commit 3395bbc

Browse files
committed
list comprehensions
1 parent 9bcf33e commit 3395bbc

File tree

4 files changed

+1679
-8
lines changed

4 files changed

+1679
-8
lines changed

comprehension_practise.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# import collections
2+
#
3+
# l1 = [10, 20, 30, 40, 50]
4+
# l2 = [10, 20, 30, 50, 40, 70]
5+
# l3 = [10, 20, 50, 40, 30]
6+
# # Counter() function counts the frequency of the items in a list
7+
# # and stores the data as a dictionary in the format <value>:<frequency>
8+
# print(collections.Counter(l1)) # Counter({10: 1, 20: 1, 30: 1, 40: 1, 50: 1})
9+
# print(collections.Counter(l2)) # Counter({10: 1, 20: 1, 30: 1, 50: 1, 40: 1, 70: 1})
10+
# print(collections.Counter(l1) == collections.Counter(l2)) # False
11+
# # True , even if order is not same also matches
12+
# print(collections.Counter(l1) == collections.Counter(l3))
13+
# print(set(l1)) #{40, 10, 50, 20, 30}
14+
# print(set(l3)) # {40, 10, 50, 20, 30}
15+
# l1.sort()
16+
# l3.sort()
17+
# print(l1 == l3) #True
18+
#
19+
# l1 = [10, 20, 30, 40, 50]
20+
# l3 = [50, 10, 30, 20, 40]
21+
# a=set(l1)
22+
# b=set(l3)
23+
# print(a,b)
24+
# print(a==b)
25+
#
26+
#
27+
# l1 = [-10, -20, 30, 40, 50]
28+
# l3 = [50, 75, 30, 20, 40, 69]
29+
# res = [x for x in l1+l3 if x not in l1 or x not in l3]
30+
# print(res)
31+
# new_list= []
32+
# for item in l1:
33+
# pos = abs(item)
34+
# new_list.append(pos)
35+
# print(new_list)
36+
37+
# x = set(['x1','rr','x3','y4'])
38+
# y = set(['x1','rr','rr','y4'])
39+
#
40+
# print ("List first: " + str(x))
41+
# print ("List second: " + str(y))
42+
#
43+
# # take difference of two lists
44+
# z = x.difference(y)
45+
#
46+
# print("Difference of first and second String: " + str(z))
47+
#
48+
# number_list = [x for x in range(100) if x%3 == 0 if x%5 == 0]
49+
# print(number_list)
50+
51+
my_list = ['91 9925479326', '18002561245', 'All the best', 'good']
52+
print([i for i in my_list if not i.startswith(('91', '18'))]) # ['All the best', 'good']

dictionaries_practise.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,29 @@
187187
# for k,v in Liverpool.items():
188188
# print(v.keys())
189189

190-
with open('employees.json') as ip:
190+
# with open('employees.json') as ip:
191+
# op = json.load(ip)
192+
#
193+
# for ele in op:
194+
# data = op['data']
195+
# for k in data:
196+
# employee_name = k['employee_name']
197+
# employee_age = k['employee_age']
198+
# profile_image = k['profile_image']
199+
# if profile_image is not None: # get employees who doesnt not have profile image
200+
# print("Employee {} doesnt have image ".format(employee_name))
201+
202+
# iterate through list of dictionaries
203+
with open('mockaroo-user-data.json') as ip:
191204
op = json.load(ip)
192205

193206
for ele in op:
194-
data = op['data']
195-
for k in data:
196-
employee_name = k['employee_name']
197-
employee_age = k['employee_age']
198-
profile_image = k['profile_image']
199-
if profile_image is not None: # get employees who doesnt not have profile image
200-
print("Employee {} doesnt have image ".format(employee_name))
207+
# store each key details in a variable
208+
first_name = ele['first_name']
209+
gender = ele['gender']
210+
last_name = ele['last_name']
211+
ip_address = ele['ip_address']
212+
print("Name is {} and IP address is {} ".format(first_name + last_name, ip_address))
213+
# import os
214+
# dir_path = os.path.dirname(os.path.abspath(__file__))
215+
# print(dir_path)

0 commit comments

Comments
 (0)