跳至内容
菜单
此问题已终结
3 回复
14662 查看

Hello All,

How to get keys and values both form dict?

For eg. i want "(u'option2', True): 3" for res.

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

Thanks in advance

形象
丢弃

Hi Pawan,

You need to use iteritems.

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

for key,value in res.iteritems():

print key, value

output:

(False, True) 1

(u'option2', True) 3

(u'option1', True) 2

Hope it helps,

Regards,

Mayank Gosai

If you want to use for python 3.

You have to use res.items() instead of res.iteritems().

For ex:

res = {(False, True): 1, (u'option2', True): 3, (u'option1', True): 2}

for key,value in res.items():

(print (key,value))

Output:

(False, True) 1

('option2', True) 3

('option1', True) 2

Thanks,

Mayank Gosai

最佳答案

Hi,

refer this:

https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary

Accept answer if helpful..

Thank you.

形象
丢弃
相关帖文 回复 查看 活动
2
11月 22
12491
0
3月 21
24
1
1月 18
15511
1
2月 25
1065
0
11月 23
1313