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

Dictionary that maps ASCII keys to their corresponding values

I'm trying to get chr() output from 65 to 90: I want to get a dictionary that looks like this: {65: 'A', 66: 'B', ..... 90: 'Z'}
-3
votes
2 answers

What's the most Pythonic way to find a matching value for any key in a dictionary?

Here is my dictionary structure: { "432701228292636694" : { "432739261603905537" : { "channels" : { "LoL Duos" : { "capacity" : 2, "rooms" : [ …
-3
votes
1 answer

How to make nested dict comprehensions in python

I have double for-loop and I would like to reformat it to dict comprehensions: tests = {'var1': {'L': True, 'C': True}, 'var2': {'L': False, 'C': True}, 'var3': {'L': False, 'C': False}} for feat, tags in tests.iteritems(): …
emcek
  • 459
  • 1
  • 6
  • 17
-4
votes
1 answer

Dictionaries python

I am making a dictionary , keys having 2 words. Suppose I have two items in the dictionary: {('am', 'and'): 1, ('and', 'am'): 1} How to identify those above 2 keys are equal (they are just in different order). for word1 in window: for word2 in…
Ayesha
  • 53
  • 7
-4
votes
1 answer

How to make dictionary comprehension in Python

I need advice about comprehensions. Got TypeError: unhashable type: 'list' but I can't see any list coming. (Trying to get dict of indexes and lengths) data = ( 'abcdefg', (10, 20, 30, 40, 50), (55,81, 33, 44) ) # TODO from this: #…
neznajut
  • 87
  • 3
  • 8
-4
votes
1 answer

Weird nested dict to dataframe

Hi I want to make a nested dictionary into a data frame 've looked up other answers for a nested dictionary to data frame but it doesn't seem to be working. d=…
-4
votes
1 answer

How to itarate over a python dictionary

I am trying to get each of the values for key symbols specifically(symbol and pricePrecision) of this Python dictionary. response = {'timezone': 'UTC', 'serverTime': 1621287738195, 'futuresType': 'U_MARGINED', 'rateLimits': [{'rateLimitType':…
-4
votes
1 answer

nested dictionary comprehension - dictionary of dictionaries

How shall I achieve the required output through dictionary comprehensions? {'R': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}}, 'L': {0: {1: 0,…
-4
votes
1 answer

get not working with dictionary comprehension

Create a dictionary containing the frequency of each character in a given string str1 = "peter piper picked a peck of pickled peppers" freq = {} freq2 = {} for c in str1: freq[c] = freq.get(c, 0) + 1 freq2 = {c:…
-4
votes
1 answer

Key-Value Assignment via list comprehension in python

For a school assignment, I have to adapt a given code but keep the dictionary comprehension mechanism in Python. Given my code dct = {k: v for k in ["HELLO", "SLEEPING"] for v in ["WORLD", "CITY"]} print(dct["HELLO"]) the printout is "CITY". How…
-4
votes
1 answer

Python List comprehension multiple if else condition

list= ["aeouis...,.,,esw","trees..,,ioee", '".....wwqqow..","...,...,,,","uouiyteerff..,,", ",w," ] I need to create a second list as an output. the output for each element will the unique vowels that are present in that element and either High:…
-4
votes
3 answers

How to count a list of words from a list in python

I have the following list: fruits = [“apple”, “banana”, “grape”, “kiwi”, “banana”, “apple”, “apple”, “watermelon”, “kiwi”, “banana”, “apple”,] Now I have to develop a function called count_the_fruits that would take as its arguments a list of fruits…
-5
votes
3 answers

using dictionary comprehension for a new dict

Been informed this has been answered - apoligies Thank you!
s322
  • 9
  • 1
  • 5
-5
votes
1 answer

Why is list comprehension so prevalent in python?

Often you see question asked about a better method of doing something, or just generally a looping question and very often the top answers will use some form of convoluted list/dict/tuple comprehension that takes longer for others to understand than…
-5
votes
5 answers

Creating keys and its value inside a dictionary using for loop

I want to create a dictionary in which each key will have names like doc1, doc2,....,doc2105 and it will store value from Title_Loans.documents[0],Title_Loans.documents[1],....,Title_Loans.documents[2104]. I am trying to run a for loop on…
Ankita Patnaik
  • 271
  • 1
  • 7
1 2 3
57
58