retrieve key of only element in a dictionary (Python 3)

Chris Angelico rosuav at gmail.com
Fri Mar 18 21:36:14 EDT 2016


On Sat, Mar 19, 2016 at 12:27 PM, Martin A. Brown <martin at linux-ip.net> wrote:
> But, I still don't understand why this works and can't puzzle it
> out.  I see a sequence on the left of the assignment operator and a
> dictionary (mapping) on the right.

When you iterate over a dictionary, you get its keys:

scores = {"Fred": 10, "Joe": 5, "Sam": 8}
for person in scores:
    print(person)

So unpacking will give you those keys - in an arbitrary order. Of
course, you don't care about the order when there's only one.

ChrisA



More information about the Python-list mailing list