How to get the first value in a Python dictionary

Modern Python 3.x’s can use iterator that has less overhead than constructing a list.

first_value = next(iter(my_dict.values()))

Note that if the dictionary is empty you will get StopIteration exception and not None.

Since Python 3.7+ this is guaranteed to give you the first item.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)