Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple key value pairs in dict comprehension

I am trying to create multiple key : value pairs in a dict comprehension like this:

{'ID': (e[0]), 'post_author': (e[1]) for e in wp_users} 

I am receiving "missing ','"

I have also tried it this way:

[{'ID': (e[0]), 'post_author': (e[1])} for e in wp_users] 

I then receive "list indices must be integers, not str"

Which I understand, but not sure the best way in correcting this and if multiple key : value pairs is possible with dict comprehensions?

like image 665
mdxprograms Avatar asked Jun 01 '14 19:06

mdxprograms


People also ask

Can dictionary keys have multiple values?

In python, if we want a dictionary in which one key has multiple values, then we need to associate an object with each key as value. This value object should be capable of having various values inside it. We can either use a tuple or a list as a value in the dictionary to associate multiple values with a key.

What is the proper way to write a List Comprehension that represents all the keys in dictionary?

Dictionary comprehension is a powerful concept and can be used to substitute for loops and lambda functions. However, not all for loop can be written as a dictionary comprehension but all dictionary comprehension can be written with a for loop.

What is the difference between List Comprehension and dict comprehension?

Its syntax is the same as List Comprehension. It returns a generator object. A dict comprehension, in contrast, to list and set comprehensions, needs two expressions separated with a colon. The expression can also be tuple in List comprehension and Set comprehension.

Why do we use dictionary comprehension in Python?

Dictionaries in Python allow us to store a series of mappings between two sets of values, namely, the keys and the values. All items in the dictionary are enclosed within a pair of curly braces {} . Each item in a dictionary is a mapping between a key and a value - called a key-value pair.


1 Answers

A dictionary comprehension can only ever produce one key-value pair per iteration. The trick then is to produce an extra loop to separate out the pairs:

{k: v for e in wp_users for k, v in zip(('ID', 'post_author'), e)} 

This is equivalent to:

result = {} for e in wp_users:     for k, v in zip(('ID', 'post_author'), e):         result[k] = v 

Note that this just repeats the two keys with each of your wp_users list, so you are continually replacing the same keys with new values! You may as well just take the last entry in that case:

result = dict(zip(('ID', 'post_author'), wp_users[-1])) 

You didn’t share what output you expected however.

If the idea was to have a list of dictionaries, each with two keys, then you want a list comprehension of the above expression applied to each wp_users entry:

result = [dict(zip(('ID', 'post_author'), e)) for e in wp_users] 

That produces the same output as your own, second attempt, but now you have a list of dictionaries. You’ll have to use integer indices to get to one of the dictionaries objects or use further loops.

like image 156
Martijn Pieters Avatar answered Sep 29 '22 17:09

Martijn Pieters