-1

i have a dictionary that includes keys as string and values as list. My question is how to return the selected keys and values using for loop?

can i save the returned result in a variable in order to print later ?

dicts = {"location":["loc1","loc2","loc3"],"category":["cat1","cat2","cat3"]} 

i tried these lines:

keys = sidebars.keys()
values = sidebars.values()
print("keys",keys)
print("values",values)

I want to return that the selected key has as values the following items.

{key1:[value1,value2],key2:[value1,value2]}
DevLeb2022
  • 653
  • 11
  • 40
  • This already has an answer here https://stackoverflow.com/questions/26660654/how-do-i-print-the-key-value-pairs-of-a-dictionary-in-python – Paulus Apr 15 '21 at 12:01

1 Answers1

0

You just have to use the item method of dictionaries.

for key, value in dicts.items():
    print("key = ", key)
    print("value = ", value)