Multiple Assignments in Python dictionary comprehension

dictionary, list-comprehension, python, python-3.x

Solution

Dictionary comprehensions build single dictionaries, not lists of dictionaries. You say you want to make a list of dictionaries, so use a list comprehension to do that.

modified_demo = [{s[0]:s[1],'Gender':s[2], 'Team':s[3]} for s in demo]

Problem

Lets say I have a list ``` demo = [['Adam', 'Chicago', 'Male', 'Bears'], ['Brandon', 'Miami', 'Male', 'Dolphins']] ``` I want to make a list of dictionaries using a comprehension that looks like ``` [{'Adam':'Chicago', 'Gender':'Male', 'Location':'Chicago', 'Team':'Bears'}, {'Brandon':'Miami', 'Gender':'Male', 'Location':'Miami', 'Team':'Dolphins'} } ``` It easy enough to assign two starting values to get something like ``` { s[0]:s[1] for s in demo} ``` but is there a legitimate way to assign multiple values in this comprehension that may look like ``` { s[0]:s[1],'Gender':s[2], 'Team':s[3] for s in demo} ``` Its such a specific question and the I dont know the terms for searching so Im having a hard time finding it and the above example is giving me a syntax error.

Original source