Dynamo Dynamo - 1 month ago 9
Python Question

I need help in understanding Python statement

Can someone help me to understand this that how this work

result = {i.key[0]: i.value[0] for i in res.item}


What does
i.key[0]: i.value[0]
do here and for example res contain a string what will be in result?
Regards

Answer

This is an expression that builds a dictionary, called a dictionary comprehension.

res.item is apparently a list of objects with two attributes key and value which seem to be lists.

The expression:

result = {i.key[0]: i.value[0] for i in res.item}

Says: for every i in res.item add to the dictionary the [0] item of the key list as the key and the [0] item of the value list as the value for the previous key.