Home > Net >  How can i get specific Element and key from python Dictionary
How can i get specific Element and key from python Dictionary

Time:06-19

i am new to python and i am having trouble on python dictionaries this is my code:

dic = {"days":["mon","tue","wed"]}
print(dic[0])

the output am expecting is

"mon","tue","wed"

But i keep getting this error :

    print(dic[0])
KeyError: 0

please help me... Thank You!

CodePudding user response:

Try:

print(dic['days']) 

You refer to values by keys. It returns a list of elements. Then, if you want a specific element, just use:

print(dic['days'][0])

...as you would do with a normal list.

CodePudding user response:

Dictionaries in python are accessed via key not index as position was not guaranteed until 3.7.

dic = {"days":["mon","tue","wed"]}
print(dic["days"])

CodePudding user response:

Thanks everyone i Found the answer

dic = {"days":["mon","tue","wed"], "months":["jan","feb","mar"]}
print(list(dic.values())[0])

the output will be:

['mon', 'tue', 'wed']

Thank you

  • Related