79757091

Date: 2025-09-05 19:14:25
Score: 1.5
Natty:
Report link

Building on the answer from @chris

The OP stated:
"...and knowing it is made of just one key/value pair..."

In this case scaling of execution time is irrelevant.

However next(iter(d)) is still faster even with a single key:value pair.

>>> import timeit
>>> setup="d = {'key': 'value'}"
>>> timeit.timeit(stmt='list(d.keys())[0]', setup=setup)
0.14511330000823364
>>> timeit.timeit(stmt='next(iter(d))', setup=setup)
0.07170239998959005
Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @chris
  • Low reputation (1):
Posted by: mph10