Questions tagged [dictionary-comprehension]

A syntactic construct in Python which provides a concise way to create dictionaries.

Dictionary comprehensions can be used to construct dictionaries in one line. For simple tasks, a dictionary comprehension may be more readable than dictionaries built using looping constructs.

Dictionary comprehensions tend to consist of an input sequence of either a list or another dictionary, variable bindings, a filtering predicate, and an output expression.

The result is a new dictionary.

Dictionary comprehensions are very similar to list comprehensions.

857 questions
3
votes
2 answers

Parsing list items and returning a dictionary using comprehension

I have a two-items list which I need to process. Those items are retrieved from a database, so the are actually header: value pairs but they are unparsed. They are strings separated by tabs, so the list looks like this: my_list =…
Chen A.
  • 10,140
  • 3
  • 42
  • 61
3
votes
5 answers

Dict Comprehension python from list of lists

I have a list of lists and I am trying to make a dictionary from the lists. I know how to do it using this method. Creating a dictionary with list of lists in Python What I am trying to do is build the list using the elements in the first list as…
mickNeill
  • 346
  • 5
  • 22
3
votes
2 answers

Average of elements in a list of list grouped by first item in the list

My list looks like my_list = [['A', 6, 7], ['A', 4, 8], ['B', 9, 3], ['C', 1, 1]], ['B', 10, 7]] I want to find the averages of the other two columns in each of the inner lists grouped by the first column in each of the inner list. [['A', 5, 7.5],…
user8508347
3
votes
2 answers

Make dict comprehension return by reference

I have following dictionary: original = {a:1, b:2} I then run dict comprehension: extracted = {k:v for (k,v) in original.items() if k == 'a'} The following dict is returned: {a:1} If I mutate extracted['a'] = 2, original['a'] will still be equal to…
user7811169
3
votes
1 answer

cpython 3.6 dict order in dict comprehension

Since in CPython 3.6 dicts are ordered (I know it's not guaranteed - yet awesome to use), so I expected the following dict comprehension to preserve the order: # attempt to get only specific k:v from dict jrn_blocks in order jrn_blocks =…
3
votes
1 answer

python: switching dictionary key structure

I have a dictionary: d = {'a1':{'b1':1, 'b2':2}, 'a2':{'b1':3, 'b2':4}}`. I want to switch the a and b keys of the dictionary. In other words, I want the resulting dictionary to be: dd = {'b1':{'a1':1, 'a2':3}, 'b2':{'a1':2, 'a2':4}} without using…
3
votes
2 answers

Process a dictionary based on types of it's value and generate another dictionary by using dictionary comprehension

Input dictionary {11: [1, 2], 23: 'ewewe', 3: [4], 41: 5, 55: 6} I need to form another dictionary based on types of items in input dictionary, it would be like :- {type: list of keys which has this type} Expected output would be {
AlokThakur
  • 3,599
  • 1
  • 19
  • 32
3
votes
1 answer

Create a dynamic nested dictionary in python using for loop

I am new to Python, and trying to learn it "on the job". And I am required to do this. Is it possible to create a 'dictionary1' dynamically which takes another 'dictionary2' as value, where 'dictionary2' is also getting updated in every for loop.…
eecs
  • 141
  • 2
  • 13
3
votes
1 answer

Populate dictionary from list in loop

I have the following code that works fine and I was wondering how to implement the same logic using list comprehension. def get_features(document, feature_space): features = {} for w in feature_space: features[w] = (w in document) …
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80
3
votes
2 answers

Dictionary comprehension within a list comprehension, per dictionary in other list

I am attempting to create a list of dictionaries, where the dictionaries inside of the list are created from a preexisting list of dictionaries, for which a new dictionary should be created from the aggregate of the key-value pairs of each…
TTT
  • 1,952
  • 18
  • 33
3
votes
1 answer

Applying dictionary comprehension to a defaultdict

I have the following, which parses three columns of tabular data (an openpyxl worksheet) into a defaultdict. def campaigns_and_adsets_and_pageviews_from_ga(ourTab): d = defaultdict(lambda: defaultdict(int)) for row in ourTab.rows[1:-1]: …
Pyderman
  • 14,809
  • 13
  • 61
  • 106
3
votes
2 answers

Creating dictionary from nested list using dictionary comprehension

Is there a way to create a dictionary with a nested list but for specific indices? I have input: data = [[int, int, int], [int, int, int], [int, int,int]] I want to so something along the lines of: my_dictionary = {} for x in data: …
3
votes
2 answers

How to make dictionary from list of lists

This is what I am doing: Where data is a list of lists in the form of [[int1, int2, int3], [int1, int2,int3]]. I want a dictionary that looks like: {int1: [int2, int3], in2:[int2, int3]}. I am checking what the size of data is before the dictionary…
3
votes
2 answers

using dict comprehension if/else logic to determine using a function or not

I had a broken function that should either wring all items through a function, or only run items that pass True from a qualifying function, if a qualifying function is given def transform_dictionary(dictionary, callback,…
codyc4321
  • 9,014
  • 22
  • 92
  • 165
3
votes
1 answer

Dictionary Comprehension from dictionary

I´m “testing” the Dictionaries Comprehensions using a dictionary to generate other. So, I want to conserve the “keys” of the first one and multiply the values *2. And yes… I want to do it with comprehension to understand. I want to reach: {4:2, 7:4,…
Peter
  • 2,004
  • 2
  • 24
  • 57