How to get the first key-value pair in a dictionary in Python

The first key-value pair of a dictionary is arbitrary because dictionaries are unordered collections. If there have been no modifications to a dictionary, order is preserved when accessing its elements.

Solution for How to get the first key-value pair in a dictionary in Python : You can use next() to get the first pair in a dictionary Call dict.items() on a dictionary to return an iterable of its key-value pairs.

Call iter(iterable) with the result of the previous step as iterable to return an iterator of the pairs.

Call next(iterator) with the result of the previous step as iterator to return the first pair as a tuple in the form (key, value).


how-to-get-the-first-key-value-pair-in-a-dictionary-in-python