Home > Mobile >  How to print out the dictionary with for loop? Python
How to print out the dictionary with for loop? Python

Time:11-01

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

CodePudding user response:

Using str.ljust and iterable unpacking:

data = {'naruto': [900, 170], 'onepiece': [600, 60]}

for movie, nums in data.items():
    print(movie.ljust(12, ' '), *nums)

naruto       900 170
onepiece     600 60
    

CodePudding user response:

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()
  • Related