Home > Enterprise >  What does the comma do when it's in front of dictionary being converted to a list
What does the comma do when it's in front of dictionary being converted to a list

Time:04-28

I have this dictionary named files I was looking for a way to get the first key without looping I read some where to convert to list and use the list to get the value from the dictionary,

files = {"file_1": "<PDF object>"}

I did this name = list(files) expected result [file_1] but I made a typo and did this instead; name, = list(files) notice the comma in front of the name variable result = file_1 What exactly does the , do I have searched for this I am yet to get an answer is this in the official python documentation?

CodePudding user response:

In python, something like a,b = 1,2 can be used to assign two variables values in one single like. Here a is being assigned the value of 1 and b is being assigned the value of 2.

It's the same as doing this,

a = 1
b = 2

In your case you wrote name, = list(files). This is something similar to the example I gave but here you only passed one variable and one value instead of two like in my example.

CodePudding user response:

when you do

a, = [b]

then the left side is a tuple:

(a,) = [b]

which sets a to b.

  • Related