Python reads through any dictionary
Here is an example dictionary.
Write a program that reads through any dictionary like this and prints the following:
(a) All the users whose phone number ends in an 8
(b) All the users that don’t have an email address listed
name_8 = []
empty_email = []
for i in range(len(d)):
c = d[i]['phone'][-1:]
if c == '8':
name_8.append(d[i]['name'])
if d[i]['email'] == '':
empty_email.append(d[i]['name'])
print(name_8)
print(empty_email)
Write a program that reads through any dictionary like this and prints the following:
(a) All the users whose phone number ends in an 8
(b) All the users that don’t have an email address listed
d=[{'name':'Todd', 'phone':'555-1414', 'email':'todd@mail.net'},
{'name':'Helga', 'phone':'555-1618', 'email':'helga@mail.net'},
{'name':'Princess', 'phone':'555-3141', 'email':''},
{'name':'LJ', 'phone':'555-2718', 'email':'lj@mail.net'}]
{'name':'Helga', 'phone':'555-1618', 'email':'helga@mail.net'},
{'name':'Princess', 'phone':'555-3141', 'email':''},
{'name':'LJ', 'phone':'555-2718', 'email':'lj@mail.net'}]
name_8 = []
empty_email = []
for i in range(len(d)):
c = d[i]['phone'][-1:]
if c == '8':
name_8.append(d[i]['name'])
if d[i]['email'] == '':
empty_email.append(d[i]['name'])
print(name_8)
print(empty_email)
SOLUTION:
['Helga', 'LJ']
['Princess']
['Helga', 'LJ']
['Princess']
Коментари
Постави коментар