I think I have a solution.
Basically, you convert the list into a string and then you play with it to make a single dictionary.
Although, I think that looping over the list is way better.
l = [{"id":1,"name":"abc"},{"id":2,"name":"def"},{"id":3,"name":"xyz"}]
#convert to string
a = str(l)
#remove parenthesis and curly brackets
a = a.replace('{','').replace('}','').replace('[','').replace(']','').replace(' ','')
#remove 'id' and 'name'
a = a.replace("'id':",'').replace(",'name'",'')
#add curly brackets
a = '{'+a+'}'
#make a dict
exec('a='+a)
>>>a
{1: 'abc', 2: 'def', 3: 'xyz'}
As you can see, you end up with a single dictionary with the right key/value pairs without using a single for loop!
SyntaxError
: there are no commas,
between dictionaries. Aside from that there is no other option. Even if you convert the list into some other structure you still need the loop for the conversion.,
. I was looking for some way using "in" operator or may be some other operator.