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

The most efficient mapping/translation (with reverse option) in Python

Consider a very large Python dictionary of the like: some_dict = { "val_1": "val_a", "val_2": "val_b", "val_x": "val_1", "val_y": "val_2", ### millions of one-to-one key-value pairs ### note that keys and values can…
SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
3
votes
1 answer

Create dataframes with names from a list

I have excel files with many tabs. I want to concat all of them, one tab at a time. I am doing: mypath = "mypath" files = os.listdir(mypath) files = [os.path.join(mypath,f) for f in files if f[-4:]=='xlsx'] sheets =…
user
  • 2,015
  • 6
  • 22
  • 39
3
votes
4 answers

How to calculate subtotal of values of dictionary for key substring matches using dictionary comprehension

I want to convert this below logic of dictionary to dictionary comprehension logic, how do I do? # Example: You have this dictA as input dictA = {'chiken_biryani': 350, 'chiken_chilli': 300, 'motton_fry': 350, 'motton_biryani': 400,…
ravibeli
  • 484
  • 9
  • 30
3
votes
3 answers

Python nested dict comprehension with if else

I am trying to convert the below to a dict comprehension my_dict = {'a': None, 'b': None, 'c': ['1', '2', '3']} new_dict = {} for k, v in my_dict.items(): if not v: new_dict[k] = None else: for item in v: …
Hemisphere
  • 35
  • 1
  • 4
3
votes
2 answers

How to convert list of tuples to dictionary with index as key

I’m trying to convert a list of tuples to a dictionary with the index of the list as its key. m = [(1, 'Sports', 222), (2, 'Tools', 11), (3, 'Clothing', 23)] So far, I’ve tried using: dict((i:{a,b,c}) for a,b,c in enumerate(m)) but this…
user6882757
3
votes
3 answers

How do I combine the following comprehension in Python?

I have two dictionaries created using dict comprehension. I am wondering if (a) there's a way to build it in a single pass, (b) is it advisable? and (c) is there a better approach? templates_gray = {k:cv2.imread(v, 0) for (k, v) in…
doubleE
  • 31
  • 1
3
votes
7 answers

How to generate a Python dictionary with simultaneous converting certain strings to floats?

Let's say I have list of lists in matrix variable: matrix = [['first', '1,1', 'last'], ['strng_1', '12231,71', 'st_2']] As you can see, all nested lists are having float data written as string. I would like to convert them to float datatype. I need…
Quanti Monati
  • 769
  • 1
  • 11
  • 35
3
votes
1 answer

Comprehension with variable number of arguments?

Can comprehensions be built programmatically to handle variable number of arguments as in Haskell? For example, how to extend this outer product for dictionaries to more than 2 dictionaries: def outer(f,g): return lambda d1,d2:…
alancalvitti
  • 476
  • 3
  • 14
3
votes
3 answers

How to get unique values in Python using list/dict comprehension

I need to get only unique values from the field "city" in my dictionary. I need to do it using list/dict comprehension. people = [ dict ( city = "Liverpool" , name = "Adam" , age = 24 ), { "city" : "New York" , "name" : "Dario" , "age" : 12 }, {…
3
votes
2 answers

Returning all possible combinations of a python dictionary in a comprehensive way

I want to return all possible key combinations of a python dictionary. In my case, it is a two-levels hierarchy dictionary. My first attempt seems like a pseudo-code-like sequence of for loops. It works but it is ugly and it gets really painful if…
asl
  • 471
  • 2
  • 4
  • 13
3
votes
2 answers

How to create dictionary from bytes (Python)

I am trying to create a list from an API call, the data comes out like this which I believe is bytes. Nothing sensitive below…
Ryan Adams
  • 31
  • 1
3
votes
4 answers

Rewrite char frequency of string as comprehension

The following procedural code snippet computes the character frequency of a text string and writes inside a dictionary. The dictionary has the characters as keys and the frequency as values. text = "asampletextstring" char_count = {} for char in…
Stefan
  • 1,151
  • 1
  • 8
  • 13
3
votes
4 answers

How to merge a list of multiple dictionaries into a dictionary of lists?

I have the following list of dictionaries in Python3.x: list_of_dictionaries = [{0:3523, 1:3524, 2:3540, 4:3541, 5:3542}, {0:7245, 1:7246, 2:7247, 3:7248, 5:7249, 6:7250}, {1:20898, 2:20899, 3:20900,…
ShanZhengYang
  • 16,511
  • 49
  • 132
  • 234
3
votes
1 answer

find number of vowels in a string

I have a string of letters as input. input: my_str = 'soumendra_in_stackoverflow' I want output like below. Where all the vowels should be printed along with their corresponding count in a dictionary. output needed: {'a': 2, 'e': 2, 'i': 1, 'o': 3,…
Soumendra
  • 1,518
  • 3
  • 27
  • 54
3
votes
1 answer

List comprehension to dictionary with multiple commands (Python, curser.execute)?

I work with Python 3.6. I have connected to Hive using Pyhive and my cursor is cur. Objective: Build a databases - tables map in a dictionary: dbname1 : list of tables within this db. I have built it using for loop but I would like to transfer it…