Accessing dict_keys element by index in Python3

前端 未结 6 864
感情败类
感情败类 2020-11-27 15:52

I\'m trying to access a dict_key\'s element by its index:

test = {\'foo\': \'bar\', \'hello\': \'world\'}
keys = test.keys()  # dict_keys object

keys.index(         


        
6条回答
  •  清酒与你
    2020-11-27 15:57

    Try this

    keys = [next(iter(x.keys())) for x in test]
    print(list(keys))
    

    The result looks like this. ['foo', 'hello']

    You can find more possible solutions here.

提交回复
热议问题