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

Iterate nested python dictionary efficiently

I'm new to python. I have a nested dictionary like (k1,(t1,v1)). I would like to loop through each and every v1 values efficiently. So lets say my dictionary is my_dict. I want to access k , x, y with out two for loops efficiently, using a…
amateur
  • 941
  • 4
  • 22
  • 33
-2
votes
1 answer

Python comprehension and data structures

I'm learning some python, and I have the following program: sentence = "the quick brown fox jumps over the lazy dog" words = sentence.split() print "\n What the hell is this???" word_lengths = [(word, len(word)) for word in words if word !=…
Alby11
  • 607
  • 1
  • 7
  • 15
-2
votes
1 answer

How to compare two values from two lists in Python

I'm trying to compare two values, one from list1 and another from list2. Each value is stored in dictionaries in these lists. The data is being read in from csv files. Currently my code is comparing only the first value and not iterating: import…
-2
votes
3 answers

Why doesn't setdefault work inside a dictionary comprehension?

Why does setdefault not increment by 1 for every occurrence in a inside a dictionary comprehension, but it does in a loop? What's going on here? Alternative solutions are great. I'm mostly interested in understanding why this doesn't work. A loop…
-3
votes
1 answer

Create a new dictionary with the key-value pair from values in a list of dictionaries based on matches from a separate list

I am trying to get a new dictionary with the k: v pair from values in a list of dictionary based on matches from a separate list. The casing is different. My data looks like this: list_of_dicts = [ {'fieldname': 'Id', 'source': 'microsoft',…
alexei7
  • 182
  • 1
  • 3
  • 11
-3
votes
1 answer

Not able to understand the output for dictionary comprehension in python

the output for the code: dict={k:v for k in range(1,4) for v in range(1,3) } print(dict) out put is: {1: 2, 2: 2, 3: 2} but thought the output should be: {1: 1, 2: 1, 3: 1} why is it taking 2 for the value of v.
-3
votes
3 answers

combine first name and last name and return object

I was trying to combine first name last name in an object and return same object again I have tried multiple ways with map and foreach but doesn't seem to work [ { "id": 1321, "createdAt": "2022-08-03T12:36:20.096Z", "UpdatedAt":…
1407
  • 3
  • 4
-3
votes
2 answers

Dictionary comprehension - dynamically generate key-value pairs?

I want to achieve the same result of {i: i+1 for i in range(4)} # {0: 1, 1: 2, 2: 3, 3: 4} But dynamically generate key: value part with myfunc(i), how do I do that? Functions that return {i: i+1} or (i, i+1) won't work: {{i: i+1} for i in…
Teddy C
  • 786
  • 10
  • 13
-3
votes
1 answer

Dictonary comphrension of another dictonary to calcualte average of list values

I have a dictonary like - d = {'a': [1, 2, 3], 'b': [4, 9], 'c': [1, 3, 9, 8]...} I want to have a new dictonary which will be r = {'a': 2, 'b': 6.5, 'c': 5.25} which are average of list items. I want to do that in a dictonary comprehension code.
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59
-3
votes
1 answer

Why my dictionary comprehension only change the first key/value

I'm new to python and I wonder why this simple code does not return all the 3 keys/values of my dictionary ? I am trying to create a dictionary and then reverse it . employees = ['Kelly', 'Emma',…
4seasonn
  • 75
  • 6
-3
votes
1 answer

Dictionary Comprehensions add in a range

I am making a programme that will read in a large dictionary. And part of its function will be to choose a number of random items from the dictionary, this is the example of the code: import random d = {'VENEZUELA': 'CARACAS', 'CANADA': 'OTTAWA',…
-3
votes
2 answers

Convert dictionary to new key:value pairs in a list of dicts

I´ve seen several questions in this matter, but haven´t been able to find a solutions to adapt in my case. I got a one level dictionary with several key:value pairs, in which the keys are names and the values are always numerical (float), like the…
-3
votes
3 answers

Update item in dict comprehension python

words = ['rocky','mahesh','surendra','mahesh','rocky','mahesh','deepak','mahesh','mahesh','mahesh','surendra'] words_count = {} for word in words: words_count[word] = words_count.get(word, 0) + 1 print(words_count) # Expected Output #…
-3
votes
1 answer

Dictionary comprehension that contains an (embedded) list comprehension

How do I return each country as the key and a list of cities in that country as the value? Using dictionary and embedded list comprehension? Without using collections country_city_tuples= [('Netherlands', 'Alkmaar'), …
-3
votes
3 answers

Remove element in dictionary of lists

I am working on Python code to remove certain elements in dictionary of lists. dict ={'s': ['a','b'],'d': ['c','d'],'g': ['e','f']} values = ['a','c','f'] list2 ={i:j.remove(value) for i,j in dict.items() for k in j for value in values if value in…
sudhir
  • 219
  • 5
  • 17