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
-1
votes
3 answers

Dictionary comprehension problem in python

A=[1,2,3,4,5] B=['a','b','c'] I want output: {1: 'a', 2: 'b', 3: 'c', 4: None, 5: None} We can have any number of keys. I want to create a dictionary with these two lists using dictionary comprehension in python and list A will become keys and…
Rohit Kumar
  • 1
  • 1
  • 4
-1
votes
2 answers

Converting a dictionary to a mathematical expression

{'YOU': {'HE': {'EST': 8, 'OLM': 6}, 'SLO': {'WLR': 8}}, 'ARE': {'KLP': {'EST': 6}, 'POL': {'WLR': 4}}, 'DOING': {'TIS': {'OIL': 8}}, 'GREAT': {'POL': {'EOL': 6}}, 'WORK': {'KOE': {'RIW': 8, 'PNG': 4}, 'ROE': {'ERC': 8, 'WQD': 6}}, 'KEEP':…
-1
votes
2 answers

How can I enter a dictionary inside an another empty dictionary?

The example code - innerdict = {} outerdict = {} for line in range(1, 10, 2): for j in range(1, 10, 2): line_tuple = ("Item" + str( line ), int( line )) key = line_tuple[1] if line ==j: outerdict[key] = dict(…
-1
votes
1 answer

Sorting the dictionary by values

sort_orders = sorted(orders.items(), key=lambda x: x[1], reverse=True) Can someone explain how key=lambda x: x[1] works to sort the dictionary by values?
-1
votes
1 answer

Count until specific condition in list of dictionary provide the mean of the value

I am trying to count the number of types before the specific code, while using a for loop. I got a list of dictionaries and I was able to count each type, but I also need to count until the code 6501 and 6502. Like the total number of type we have…
kavi
  • 3
  • 2
-1
votes
1 answer

Convert str command output to a dictionary

I get an output of a command like below in str: Group Name: STI IP: v4 Comment: Group Items: 2 Type: User-defined Members: "vm-1" "vm-2" I want to be able to convert it into a dictionary so that I get a…
Satnau
  • 69
  • 1
  • 9
-1
votes
1 answer

Trouble with dictionary comprehension and conditions

I have a dictionary with the following structure: {1: ["string1", 10, "string2", 5.0], 2: ["string3", 200, "string4", 7.5]} My goal is to create a function which iterates through that dictionary and overwrites the numeric values based on whether…
fjv94
  • 55
  • 1
  • 5
-1
votes
2 answers

Merge two dictionaries conditionally using dict comprehension

I'd like two join two dictionaries based on the value of d1 and a substring of the key of d2. The resulting dictionary has the key of d1 with the corresponding value of d2. d1 = {'web02': '23', 'web01': '50'} d2 = {'server/dc-50': 's01.local',…
-1
votes
2 answers

Is it possible to extract two values with one Dictionary comprehension?

With a list comprehension like this, it is possible to extract a specific value from a key in a list of dictionaries: ke = [d['_text'] for d in ls if '_text' in d] Is it possible to extract two values at a time and store them as a tuple with one…
gython
  • 865
  • 4
  • 18
-1
votes
1 answer

invalid syntax when trying to make a dictionary comprehension?

test_list_k = ['a', 'b', 'c', 'd'] test_list_v = ['f', 'g', 'h'] test_dict_comp = {k: v for k in test_list_k and for v in test_list_v} print(test_dict_comp) For some reason it counts the second for in test_dict_comp as invalid syntax.
-1
votes
1 answer

Python: Convert table to string to key:value pairs and store in nested dict using comprehension

I received data from a subprocess command as a string. I want to store this data in a nested dict using dictionary comprehension. How do I do this? Note: Table rows and columns will vary for each subprocess command. This is only an example: (I have…
suspense_hey
  • 85
  • 2
  • 11
-1
votes
3 answers

How to assign keys and save it as dictionary in python?

I am new to python. I was working with a CSV file which looks something like: man,nut,bag rat,cat dog,fog,cat,man Thing is, I want to assign a unique number to each of these values in the csv file such that the unique number acts as the key and the…
Rocky4
  • 25
  • 6
-1
votes
1 answer

How does Python manage to loop through values in a dictionary?

I want to know how Python loops through values in a dictionary. I know how to do it in code, and all of the answers I have read just explain how to do it. I want to understand how python finds the values, as I thought that dictionary values were…
-1
votes
1 answer

Difference in structure nested lists and nested dictionaries

I was trying to get a nested dictionary comprehension to work after I found out how a nested list comprehension worked. I succesfully created the nested dictionary comprehension, although I'm still confused why the syntax is different. In the nested…
Michael
  • 1,281
  • 1
  • 17
  • 32
-1
votes
3 answers

Create a dictionary from two lists having duplicate elements

Create a dictionary from two lists having duplicate elements where list 1 is the key of the new dictionary and for all strings in list 2 that exactly match list 1, append it to its values field for the keys in list 1. So, for…
Swordsman
  • 143
  • 1
  • 2
  • 14