-1
pyDic= {'1': 'MEMBER1', '2': 'MEMBER2' ,'3': 'MEMBER3', '4': 'MEMBER4' , '5': 'MEMBER5'}
print(pyDic.keys(1))
David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • 1
    Hi. I encourage you to read the documentation of python dictionaries first. dict.keys() gives a view on the dictionary on which we can't write stg like .keys(1). we can't write something like pyDic.keys(1) as .keys() doesn't take any arguments. https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects You can do the trick by just next(pyDic.keys()) for more things regarding next() and iterators please read the documentation – Akash Tadwai Sep 15 '22 at 17:29
  • @rdas That will induce a TypeError – DarkKnight Sep 15 '22 at 17:32
  • Does this answer your question? [How do you find the first key in a dictionary?](https://stackoverflow.com/questions/30362391/how-do-you-find-the-first-key-in-a-dictionary) – gre_gor Sep 15 '22 at 17:56

1 Answers1

0

To get the first value from a dictionary you can do this:

pyDic= {'1': 'MEMBER1', '2': 'MEMBER2' ,'3': 'MEMBER3', '4': 'MEMBER4' , '5': 'MEMBER5'}

print(next(iter(pyDic.values())))

The dictionary function values() returns a reference to a dict_values class. This can be converted to an iterator with iter() then call next() on the iterator to get the first value.

Another option is to create a list from the dict_values class and then take the 0th element as follows:

print(list(pyDic.values())[0])

You can also unpack as follows:

value, *_ = pyDic.values()
print(value)

Output:

MEMBER1
DarkKnight
  • 19,739
  • 3
  • 6
  • 22