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
-2
votes
1 answer

Conversion of list into a dictionary

How shall I convert a string as given below into a dictionary in the given way? sentence = "python is a programming language" Output: {'p' : ['python', 'programming'], 'i' : ['is'], 'a' : ['a'], 'l' : ['language']}
Dynamite
  • 1
  • 1
-2
votes
2 answers

Can't crate a dictionary from two lists using dictionary comprehension with two FORs. Why?

I know that there are a bunch of ways to make a dictionary out of two lists, but I wanted to do it using two FOR loops to iterate over both lists. Therefore, I used the following code. Surprisingly, the code doesn't iterate over the second list that…
-2
votes
2 answers

how to set values in a dict from a dataframe in python

I have a dataframe which looks like this: code to create the df: dd = {'name': ["HARDIE'S MOBILE HOME PARK", 'CRESTVIEW RV PARK', 'HOMESTEAD TRAILER PARK', 'HOUSTON PARK MOBILE HOME PARK', 'HUDSON MOBILE HOME PARK', 'BEACH DRIVE…
brenda
  • 656
  • 8
  • 24
-2
votes
1 answer

UnboundLocalError: local variable referenced before assignment within comprehension

I'm playing around with dict comprehensions, and trying PEP572 (the := operator), in an example like this: columns = {'idx', 'class_name'} # populate them somehow # ... retval = {names[idx]:idx for idx in range(len(names)) if (names :=…
Ælex
  • 14,432
  • 20
  • 88
  • 129
-2
votes
1 answer

Find a key starting from a value in a dictionary that have lists as values

Like the title explains, let's say I have a dictionary like this: dictionary = {apple: ["red", "healthy"], fries: ["yellow", "not healthy"], wall: ["is that eatable?"]} The length of each list can change but we know for sure that each value (all…
Krist
  • 31
  • 6
-2
votes
1 answer

Dictionary Comprehension with a set of Tuple as value

I have this dict_1 = {1:{("Apple",1),("Orange",1),("Banana",1),("Lemon",1)}} And i would like this : {"Apple","Orange","Banana","Lemon"} What would be the correct comprehension here. My attempts failed because of TypeError: unhashable type:…
Easytoday
  • 1
  • 4
-2
votes
1 answer

Convert dictionary values to be inside a list

I´m trying to convert the values inside a dictionary to be in a list (for future upload in mySQL). So currently I have the dictionary like below: old_dict = {'key1':'value1','key2':value2,'key3':'value3'} And I´m looking for something…
-2
votes
2 answers

Dict Key and Value populated from splitting each item in a python list

I am trying to split this list into two lists then to populate it into a dictionary for keys and items. I have a list of Statements for each industry and would like to split this list into two lists. 1 with the statements and 1 with the industries.…
-2
votes
2 answers

How to convert list of strings to list of dictionaries in python

Suppose I have a list of strings in Python: ['Name: volume_test_add_volume_to_cg_2019_03_07-12_21_37', 'Index: 24', 'Name: volume_xx111', 'Index: 3', 'Name: volume_xx11541', 'Index: 4', 'Name:…
-2
votes
1 answer

Creating a "compressed" dictionary

I have a python dictionary that I want to compress into a diffrent dict based on the keys: {'field1_0': 'FieldName1', 'field2_0': 'DataType1', 'field1_1': 'FieldName 2', 'field2_1': 'DataType2'} In this post my keys are automated form field…
ViaTech
  • 2,143
  • 1
  • 16
  • 51
-2
votes
1 answer

How to convert this for loop into a dict comprehension?

I can achieve this with mydict = {} for a in range(0,6): mydict[a] = [] print (mydict) #{0: [], 1: [], 2: [], 3: [], 4: [], 5: []} Question is how would I achieve this with dict comprehension? Edit: d = {level: [] for level in range(1, level…
user10514708
-2
votes
3 answers

Making a dictionary with keys being lists of increasing values

I'm trying to figure out how to make a dictionary that looks like this: 1: [0, 1, ..., 26] 2: [27, 29, ..., 53] 3: [55, 56, ..., 80] I can't think of a quick way to do this. Can anybody think of an elegant way to code a dictionary that looks like…
-2
votes
1 answer

Why isn't this nested dict comprehension working in Python?

nested_dict = { b: { a: some_other_source_dict[b][a] or {} for a in a_list } for b in b_list } If some_other_source_dict[b][a] exists, the correct output should be: nested_dict = { b_key_1: { a_key_1: a_val_1, a_key_2: a_val_2 }, …
Ice101781
  • 105
  • 10
-2
votes
1 answer

Dictionary comprehension to find all numbers b/w 1 and 25 that are divisible by a single digit other than 1 (2-9)

d={k:v for k in range(1,25) for v in range(2,9) if k%v==0} print (d) d={newdic.setdefault(key,value) for key in range(1,25) for value in range(2,9) if key%value==0} print (newdic) output of above code is {2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8,…
Sel_Python
  • 191
  • 2
  • 7
  • 16
-2
votes
1 answer

Is this Dictionary comprehension possible?

Im trying to create a dictionary with a dictionary comprehension. The dict should look like this: So far I tried: player_objects = ['list', 'of', 'player_objects'] extended_data = {player_name:{'Money':100, 'Items'=['Item1', 'Item2']}} data =…
optimalic
  • 511
  • 3
  • 17