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
4 answers

Converting a list into a multi-valued dict

I have a list as such: pokemonList = ['Ivysaur', 'Grass', 'Poison', '', 'Venusaur', 'Grass', 'Poison', '', 'Charmander', 'Fire', ''...] Note that the pattern is 'Pokemon name', 'its type', ''...next pokemon Pokemon come in both single and dual type…
avereux
  • 572
  • 1
  • 5
  • 15
3
votes
2 answers

iterate inside a dictionary comprehension statement

I,m recently started learning python, so I'm sure that are many things that I don't know that may be quite simple to solve. However, searching through a lot of questions I couldn't find an answer to this one. Is it possible to iterate an variable in…
Hugo bler
  • 31
  • 2
3
votes
4 answers

Turning a list of dictionaries into a list of lists

I know this is possible with list comprehension but I can't seem to figure it out. Currently I have a list of dictionaries like so: [ {'field1': 'a', 'field2': 'b'}, {'field1': 'c', 'field2': 'd'}, {'field1': 'e', 'field2': 'f'} ] I'm trying…
3
votes
2 answers

Python Dict Comprehension to Create and Update Dictionary

I have a list of dictionaries (data) and want to convert it into dictionary (x) as below. I am using following ‘for loop’ to achieve. data = [{'Dept': '0123', 'Name': 'Tom'}, {'Dept': '0123', 'Name': 'Cheryl'}, {'Dept': '0123',…
akshat
  • 1,219
  • 1
  • 8
  • 24
3
votes
5 answers

dict comprehensions for mapping scrabble letter distribution

I have a list namely: scrabble_scores = [(1, "E A O I N R T L S U"), (2, "D G"), (3, "B C M P"), (4, "F H V W Y"), (5, "K"), (8, "J X"), (10, "Q Z")] I have to write a function which gives a dict which contains a letter to score…
Ramya
  • 313
  • 1
  • 4
  • 10
3
votes
2 answers

Refactoring with python dictionary comprehension

I have 2 dictionary which contain the same keys but the value pairs are different. Let's make dictA and dictB represent the two dictionaries in question. dictA = {'key1':'Joe', 'key2':'Bob'} dictB = {'key1':'Smith', 'key2':'Johnson'} Currently, I…
Michael Markieta
  • 432
  • 4
  • 16
3
votes
6 answers

Dictionary comprehension with conditional

So I'm wondering if anyone can help me out with this issue I'm having. Lets assume I have a dictionary: d = {1: {2: 3}, 4: 5} I want to create a dictionary of any contained dictionaries: wanted_result = {2: 3} what I am trying is this: e =…
spdub
  • 45
  • 1
  • 5
2
votes
2 answers

Prepend a prefix to every element of a list as dictionary values

I have this dict: {'q1': [5, 6, 90, 91, 119, 144, 181, 399], 'q2': [236, 166], 'q3': [552, 401, 1297, 1296], } And I'd like to prepend a 'd' to every element within each value's list: {'q1': ['d5', 'd6', 'd90', 'd91', 'd119', 'd144', 'd181',…
Hefe
  • 421
  • 3
  • 23
2
votes
1 answer

Python dictionary pointwise output

if i have a dictionary: d={ 's':['a','b','c'], 'v':['d','e'], 'r':['g','h','i'],} (which could have an arbitrary number of keys) how can i output a list of all the pair-wise combinations i.e. ['adg','adh', 'adi', 'aeg', 'aeh', 'aei', 'bdg',…
2
votes
4 answers

Change keys in a dictionary

I have this dictionary. maximo = {'CodChamado': 50, '_14984|Top Down:': 0, '_14985|Hierarquia solicitante:': 0} And I want to change these keys "_14984|Top Down:" and "_14985|Hierarquia solicitante:" to new_key = ['Campo Extra|Top Down:', 'Campo…
2
votes
2 answers

Python itertools.groupby with dictionaries with multiple values

I am trying to use the Python itertools.groupby function to change this list: items = [ {'price': 5.0, 'name': 'Strawberries'}, {'price': 5.0, 'name': 'Strawberries'}, {'price': 5.0, 'name': 'Strawberries'}, {'price': 11.23, 'name':…
2
votes
2 answers

How to implement comprehension in a class

As far as I know, comprehensions in Python only work with lists, dictionaries, tuples and sets. There is nothing on comprehension in the Python data model. The syntax for tuples is quite interesting >>> tuple(i for i in range(3)) (0, 1,…
edd313
  • 1,109
  • 7
  • 20
2
votes
3 answers

Dictionary from another dictionary

I have a dictionary with IDs and counts: items_count = { '1111:271': 111, '1111:190': 3, '1231:106': 13, '1211:104': 111, '1111:201': 9 } Key is "category":"id". I want to separate category and put it in another…
2
votes
2 answers

Append to list in python dict comprehension

Suppose we have a dictionary inp = {"virat":60,"rohit":50,"sardhul":50,"rana":60} and we should get the output as {60: ['virat', 'rana'], 50: ['rohit', 'sardhul']} I can do it in normal python programming as follows out = dict() for key, val…
2
votes
1 answer

How to create a dictionary of series with an index from a dataframe in python

I have the following dataframe df = pd.DataFrame({'col_1': ['Animal','Deer','Sheep','Fish','Vehicle','Truck','Car','Mineral','Gold','Silver'], 'col_2': ['Animal',0.5,0.25,0.25,'Vehicle',0.5,0.5,'Mineral',0.75,0.25], …
Jonathan Hay
  • 195
  • 11