How to print both strings in a dictionary in Python
Question:
I’m having trouble printing both the name in the list and the email. The beginning part of the code was already pre written and my attempt is the for loop. If anybody would be able to help me that would be greatly appreciated.
Here are the directions:
Write a for loop to print each contact in contact_emails. Sample output for the given program:
[email protected] is Mike Filt
[email protected] is Sue Reyn
[email protected] is Nate Arty
Code:
contact_emails = {
'Sue Reyn' : '[email protected]',
'Mike Filt': '[email protected]',
'Nate Arty': '[email protected]'
}
for email in contact_emails:
print('%s is %s' % (email, contact_emails(email)))
Answers:
Your problem is that you need to use square brackets([]
) instead of parenthesis(()
). Like so:
for email in contact_emails:
print('%s is %s' % (contact_emails[email], email)) # notice the []'s
But I recommend using the .items()
( that would be .iteritems()
if your using Python 2.x) attribute of dict
s instead:
for name, email in contact_emails.items(): # .iteritems() for Python 2.x
print('%s is %s' % (email, name))
Thanks to @PierceDarragh for mentioning that using .format()
would be a better option for your string formatting. eg.
print('{} is {}'.format(email, name))
Or, as @ShadowRanger has also mentioned that taking advantage of prints variable number arguments, and formatting, would also be a good idea:
print(email, 'is', name)
A simple way to do this would be using for/in loops to loop through each key, and for each key print each key, and then each value.
Here’s how I would do it:
contact_emails = {
'Sue Reyn' : '[email protected]',
'Mike Filt': '[email protected]',
'Nate Arty': '[email protected]'
}
for email in contact_emails:
print (contact_emails[email] + " is " + email)
Hope this helps.
for email in contact_emails:
print("{} is {}".format(contact_emails[email], email))
I’m having trouble printing both the name in the list and the email. The beginning part of the code was already pre written and my attempt is the for loop. If anybody would be able to help me that would be greatly appreciated.
Here are the directions:
Write a for loop to print each contact in contact_emails. Sample output for the given program:
[email protected] is Mike Filt
[email protected] is Sue Reyn
[email protected] is Nate Arty
Code:
contact_emails = {
'Sue Reyn' : '[email protected]',
'Mike Filt': '[email protected]',
'Nate Arty': '[email protected]'
}
for email in contact_emails:
print('%s is %s' % (email, contact_emails(email)))
Your problem is that you need to use square brackets([]
) instead of parenthesis(()
). Like so:
for email in contact_emails:
print('%s is %s' % (contact_emails[email], email)) # notice the []'s
But I recommend using the .items()
( that would be .iteritems()
if your using Python 2.x) attribute of dict
s instead:
for name, email in contact_emails.items(): # .iteritems() for Python 2.x
print('%s is %s' % (email, name))
Thanks to @PierceDarragh for mentioning that using .format()
would be a better option for your string formatting. eg.
print('{} is {}'.format(email, name))
Or, as @ShadowRanger has also mentioned that taking advantage of prints variable number arguments, and formatting, would also be a good idea:
print(email, 'is', name)
A simple way to do this would be using for/in loops to loop through each key, and for each key print each key, and then each value.
Here’s how I would do it:
contact_emails = {
'Sue Reyn' : '[email protected]',
'Mike Filt': '[email protected]',
'Nate Arty': '[email protected]'
}
for email in contact_emails:
print (contact_emails[email] + " is " + email)
Hope this helps.
for email in contact_emails:
print("{} is {}".format(contact_emails[email], email))