Accessing dict_keys element by index in Python3

前端 未结 6 845
感情败类
感情败类 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:55

    Not a full answer but perhaps a useful hint. If it is really the first item you want*, then

    next(iter(q))
    

    is much faster than

    list(q)[0]
    

    for large dicts, since the whole thing doesn't have to be stored in memory.

    For 10.000.000 items I found it to be almost 40.000 times faster.

    *The first item in case of a dict being just a pseudo-random item before Python 3.6 (after that it's ordered in the standard implementation, although it's not advised to rely on it).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 16:02

    I wanted "key" & "value" pair of a first dictionary item. I used the following code.

     key, val = next(iter(my_dict.items()))
    
    0 讨论(0)
  • 2020-11-27 16:06
    test = {'foo': 'bar', 'hello': 'world'}
    ls = []
    for key in test.keys():
        ls.append(key)
    print(ls[0])
    

    Conventional way of appending the keys to a statically defined list and then indexing it for same

    0 讨论(0)
  • 2020-11-27 16:11

    Call list() on the dictionary instead:

    keys = list(test)
    

    In Python 3, the dict.keys() method returns a dictionary view object, which acts as a set. Iterating over the dictionary directly also yields keys, so turning a dictionary into a list results in a list of all the keys:

    >>> test = {'foo': 'bar', 'hello': 'world'}
    >>> list(test)
    ['foo', 'hello']
    >>> list(test)[0]
    'foo'
    
    0 讨论(0)
  • 2020-11-27 16:12

    In many cases, this may be an XY Problem. Why are you indexing your dictionary keys by position? Do you really need to? Until recently, dictionaries were not even ordered in Python, so accessing the first element was arbitrary.

    I just translated some Python 2 code to Python 3:

    keys = d.keys()
    for (i, res) in enumerate(some_list):
        k = keys[i]
        # ...
    

    which is not pretty, but not very bad either. At first, I was about to replace it by the monstrous

        k = next(itertools.islice(iter(keys), i, None))
    

    before I realised this is all much better written as

    for (k, res) in zip(d.keys(), some_list):
    

    which works just fine.

    I believe that in many other cases, indexing dictionary keys by position can be avoided. Although dictionaries are ordered in Python 3.7, relying on that is not pretty. The code above only works because the contents of some_list had been recently produced from the contents of d.

    Have a hard look at your code if you really need to access a disk_keys element by index. Perhaps you don't need to.

    0 讨论(0)
提交回复
热议问题