-1

I tried something like this :

for key in dict_of_ims:
    for element in key:
        print(element)  

it just gave the keys letter by letter

lara
  • 51
  • 7

5 Answers5

3

Use values:

print(dict_of_ims.values())

Or to unpack the values:

for val in dict_of_ims.values():
    for item in val:
        print(item)
jspcal
  • 50,847
  • 7
  • 72
  • 76
1

If you want to print all the values of a list which is value for each key in a dictionary:

for key, value in dict1.items():
    for ele in value:
        print(ele)

Example:

dict1 = {'milk': ['gallons', 5, 10, 50], 'name': ['hari']}

Output:

gallons

5

10

50

hari

  • And if you code in Eclipse, you can hide warning: for _, value in dict_of_ims.items(): – Antti A May 16 '18 at 09:47
  • maybe my question wasn't accurate, my keys has lists i want each element of each list in each key to be printed seperately not an entire list at once. – lara May 16 '18 at 09:49
  • Thanks @lara. understood the question now. I will update it –  May 16 '18 at 09:51
  • @lara Now please check and confirm me Is this you are expecting ? –  May 16 '18 at 09:55
0
for key in dict_of_ims:
    print(dict_of_ims[key])  
Antti A
  • 692
  • 5
  • 10
  • maybe my question wasn't accurate, my keys has lists i want each element of each list in each key to be printed seperately not an entire list at once. – lara May 16 '18 at 09:49
0

In dict values method is there print(dict_of_ims.values()) will return list of values print(dict_of_ims.keys()) will return list of keys

ashish pal
  • 467
  • 4
  • 10
  • maybe my question wasn't accurate, my keys has lists i want each element of each list in each key to be printed seperately not an entire list at once. – lara May 16 '18 at 09:49
0

Based on your comments on other answers, your dictionary will look something like this:

d = {'a': [1, 2, 3, 4], 'b': [7, 8, 9]}

Now you can use chain from itertools to get your desired output:

from itertools import chain
values = list(chain.from_iterable(d.values()))
print(values)

Output:

[1, 2, 3, 4, 7, 8, 9]

You can further, format it based on your requirement:

>>> print(" ".join(values))
1 2 3 4 7 8 9
>>> print("\n".join(values))
1
2
3
4
7
8
9
akshat
  • 1,219
  • 1
  • 8
  • 24