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

Summing Dictinary Values

I have a list of dictionaries like this` [ { "account_name": "Rounded Off (Purchase)", "amount": 0.28, "doc_date": "2023-04-05", "doc": "P.Inv.-1", "date_created": "2023-04-05T15:30:42.964203" }, { …
-2
votes
1 answer

Why is the value not getting added to the dictionary value?

capacity = [10,2,2] rocks = [2,2,0] a=100 bags= {c:r for (c,r) in zip(capacity,rocks)} print(bags) ''' {10: 2, 2: 0} ''' I want to get {10: 2, 2:2, 2: 0} but instead I am getting {10: 2, 2: 0}
-2
votes
1 answer

Cycle for to add to dictionary from file in a comprehension view

I have a file contains this (actually it has much more lines it is only a…
-2
votes
3 answers

Why dictionary comprehention gives only 3 elements

Why does this code only give three elements as output? num_list1 = [6,34,-55,65,2,0,-6,77,66,55,44,33] new_dict = {'positive' if key > 0 else 'negative' if key < 0 else 'zero': key for key in num_list1} print(new_dict) Output: {'positive': 33,…
-2
votes
1 answer

better way to pass dictionary values in a function

def my_function(): payload = { "key1": f"{value1}", "key2": "default value", "propertySet": [ { "key3": f"{value3}", "key4": f"{value4}", …
-2
votes
6 answers

Python Dictionary in Comprehension

Given a Dictionary: operating_hrs = {'MONDAY': 8, 'TUESDAY': 6, 'WEDNESDAY': 5, 'THURSDAY': 8, 'FRIDAY': 3, 'SATURDAY': 5.25, 'SUNDAY': 0} day = "FRIDAY" I want to get the value of FRIDAY which is 3. But I got an error or empty value. Try #1: [v…
iBoon
  • 13
  • 4
-2
votes
1 answer

List Comprehension - local variable referenced before assignment error

I have a source list of images which looks like this (simplified for brevity): images = [ { 'url': 'https://example.com/image.jpg', 'description': 'Caption', 'type': 'photograph', 'order': 1 }, { …
ianyoung
  • 2,863
  • 4
  • 26
  • 31
-2
votes
1 answer

Explain this Dict comprehension python

dict1={"a":4,"b":2,"A":6} print({k.lower():dict1.get(k.lower(),0) + dict1.get(k.upper(),0) for k in dict1.keys()}) I copied this code from a youtube video. I couldn't understand the code properly. Please help me to figure it out. I couldn't…
Muaviya
  • 1
  • 3
-2
votes
2 answers

Removing nested list in a dictionary

This is my input data. data = {323: [[639, 646]], 325: [[1491, 1507]], 332: [[639, 647], [823, 833], [1274, 1298]], 334: [[640, 646]], 335: [[822, 834]]} I want to remove the sublist whose values are lesser than or equal to 10. My output would look…
-2
votes
2 answers

Python Dictionary Comprehension Not Outputting As Expected

I am playing around with dictionaries, and thought how would I create a dictionary using comprehensions. I thought {k:v for k in [0,1,2] for v in [5,8,7]} would print as {0:5, 1:8, 2:7} But instead it prints as {0: 7, 1: 7, 2: 7} Why is this…
-2
votes
2 answers

Populate Python dictionaries with pre-assigned keys and values of a particular length

Say for example I have the following dictionary in Python: memory_map = {'data': [1,2,3], 'theta': [4,5,6,7]} I would like to create another random dictionary that is identical to memory_map, has the same keys and the same lengths of lists as their…
-2
votes
2 answers

Is it possible to use python dict comprehension when updating values?

counts = defaultdict(int) for elem in sets: #list of sets for _ in elem: #each set contains elements that may be duplicates among the sets counts[_] += 1 Is there a way to use dict comprehension for code like this?
-2
votes
2 answers

How to merge two dictionaries and setting new key values to 0?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. I need to have to following input and output Let's say we compare two sentences "I like potatoes" and "I like…
-2
votes
1 answer

How can I create an empty triple nested dictionary using dictionary comprehension?

I have three lists that I would like to make into keys for an empty triple nested dictionary using dictionary comprehension. "Facility" is the outer key, "Products" is the middle key, and "Customer" is the inner key. How can I do this? Thanks in…
Phil
  • 31
  • 3
-2
votes
1 answer

How to merge and create dict of dicts from a dictionary

I have a dictionary like the one below in which the value of one of the list elements will be a key somewhere in the same dictionary. {"a": ["b", "c"], "b": ["D"], "c": ["A", "B", "C"], "A": ["abc", "aab", "aba"], "B": ["bcd", "bdc"], "C": ["dab",…
Shikhar
  • 93
  • 7