Convert a list of dictionaries to a dictionary of dictionaries I need to convert a list of dictionaries to a dictionary of dictionaries, with a particular item as the new key.

This is the way I'm currently doing it:

&gt;&gt;&gt; a
[{'bar': 'baz', 'id': 'foo'}, {'bar': 'baz', 'id': 'qux'}]
&gt;&gt;&gt; {
    item['id']:{
        sub_item:item[sub_item] for sub_item in item if sub_item != 'id'
    } for item in a}

{'qux': {'bar': 'baz'}, 'foo': {'bar': 'baz'}}


However, this looks really ugly to me.  Is there a more elegant way to do this?
While using methods with side effects in list- or dict-comprehensions is generally frowned upon, in this case you could make use of dict.pop to get the id and at the same time remove it from the dictionary.

&gt;&gt;&gt; a = [{'bar': 'baz', 'id': 'foo'}, {'bar': 'baz', 'id': 'qux'}]
&gt;&gt;&gt; {d.pop("id"): d for d in a}
{'qux': {'bar': 'baz'}, 'foo': {'bar': 'baz'}}


Keep in mind, though, that this will alter the dictionary in-place, i.e. afterwards:

&gt;&gt;&gt; a
[{'bar': 'baz'}, {'bar': 'baz'}]


If you do not want to alter the original dictionaries, use map(dict, a) to create copies before poping elements from those, leaving the original dicts in a as they were.

&gt;&gt;&gt; {d.pop("id"): d for d in map(dict, a)}
{'qux': {'bar': 'baz'}, 'foo': {'bar': 'baz'}}