How to print out the dictionary with for loop? Python
Question:
I would like to print the item of dictionary. how to do that?
this is my code
{'naruto': [900, 170], 'onepiece': [600, 60]}
for key, value in stock_dict1.items():
for value in stock_dict1.values():
print(key, value[0], value[1])
when I print out, the result will be like this:
how to do that?
naruto 900 170
onepiece 600 60
Answers:
Using str.ljust
and iterable unpacking:
data = {'naruto': [900, 170], 'onepiece': [600, 60]}
for movie, nums in data.items():
print(movie.ljust(11, ' '), *nums)
naruto 900 170
onepiece 600 60
The following code snippet provides the easy way for beginner.
Try this:
d = {'naruto': [900, 170], 'onepiece': [600, 60]}
for key in d:
print(key, end=' ')
for value in d[key]:
print(value, end=' ')
print()
I would like to print the item of dictionary. how to do that?
this is my code
{'naruto': [900, 170], 'onepiece': [600, 60]}
for key, value in stock_dict1.items():
for value in stock_dict1.values():
print(key, value[0], value[1])
when I print out, the result will be like this:
how to do that?
naruto 900 170
onepiece 600 60
Using str.ljust
and iterable unpacking:
data = {'naruto': [900, 170], 'onepiece': [600, 60]}
for movie, nums in data.items():
print(movie.ljust(11, ' '), *nums)
naruto 900 170
onepiece 600 60
The following code snippet provides the easy way for beginner.
Try this:
d = {'naruto': [900, 170], 'onepiece': [600, 60]}
for key in d:
print(key, end=' ')
for value in d[key]:
print(value, end=' ')
print()