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
5
votes
2 answers

single line if else in python dictionary comprehension method

I just tried a list comprehension like this [i if i==0 else i+100 for i in range(0,3)] and it worked, but when I tried a similar dict comprehension, it throws an error: d={3:3} {d[i]:0 if i==3 else d[i]:True for i in range(0,4) } What could be the…
syam
  • 387
  • 4
  • 19
5
votes
1 answer

Python: dict comprehension with multiple key-value pairs in the same time

The goal: working analogue of expression {k1: v1, k2: v2 for k1, k2, v1, v2 in data} or more particular case {k1: v, k2: v for k1, k2, v, _ in data} that iterates through data only 1 time (in given examples data is iterable object of…
5
votes
1 answer

Swift dictionary comprehension

Other languages such as Python let you use a dictionary comprehension to make a dict from an array, but I haven't figure out how to do this in Swift. I thought I could use something like this but it doesn't compile: let x = ["a","b","c"] let y =…
Yusuf X
  • 14,513
  • 5
  • 35
  • 47
5
votes
1 answer

Comprehensions with multiple input sets

I'm experimenting with python and am stuck trying to understand the error messages in the context of what I am doing. I'm playing around with comprehensions and trying to find a pattern to create a list/dictionary comprehension with more than one…
5
votes
2 answers

Python dictionary comprehension example

I am trying to learn Python dictionary comprehension, and I think it is possible to do in one line what the following functions do. I wasn't able to make the n+1 as in the first or avoid using range() as in the second. Is it possible to use a…
stenci
  • 8,290
  • 14
  • 64
  • 104
5
votes
1 answer

Invalid syntax using dict comprehension

Given a list of floats named 'x', I would like to create a dict mapping each x in x[1:-1] to it's neighbors using a dict comprehension. I have tried the following line : neighbours = {x1:(x0,x2) for (x0,x1,x2) in zip(x[:-2],x[1:-1],x[2:])} However,…
Chris
  • 531
  • 5
  • 11
4
votes
3 answers

Python dict comprehension convert list of tuples of dictionaries and integers into list of dictionaries

I have list of dictionaries and list of integers x = [ { "name": "tom", "job": "judge" }, { "name":"bob", "job": "policeman" } ] y = [1000, 2200] I want to zip them and add y elements to dictionaries…
M.wol
  • 901
  • 3
  • 10
  • 21
4
votes
1 answer

How does python dict comprehension work with lambda functions inside

My goal is to aggregate a pandas DataFrameGroupBy Object using the agg function. In order to do that, I am generating a dictionary that I'm going to unpack to kwargs using dict unpacking through **dict. This dictionary is required to contain the new…
Robin
  • 125
  • 5
4
votes
4 answers

Dictionary comprehension with multiple values for each key

Im doing a course in bioinformatics. We were supposed to create a function that takes a list of strings like this: Motifs =[ "AACGTA", "CCCGTT", "CACCTT", "GGATTA", "TTCCGG"] and turn it into a count matrix that counts…
4
votes
2 answers

How to readably and efficeintly create a dictionary of iterable combinations with a tuple of their indices as keys?

So i have some code that works, but it is at best hard to read, and I feel inefficient as it uses two list comprehensions where a single one should suffice. What I need is to create a dictionary of all n combinations of the letters in alpha, with…
4
votes
4 answers

How to fill a dictionary in python?

I have a list of lists like so: N = [[a,b,c],[d,e,f],[g,h,i]] I would like to create a dictionary of all the first values of each list inside N so that I have; d = {1:[a,d,g],2:[b,e,h],3:[c,f,i]} I have tried many things and I cant figure it out.…
4
votes
1 answer

Why are generator expressions called that?

On one hand we have List comprehension [x for x in range(10)] Set comprehension {x for x in range(10)} Dict comprehension {x: x for x in range(10)} On the other we have Generator expression (x for x in range(10)) Why are the first three…
4
votes
2 answers

How to create and increment a value in a dictionary from a list using dictionary comprehension

I'm creating a simple function that takes a list of numbers and returns a dictionary with the sum of all the odd and even numbers. I'm able to do so with a traditional for loop, so I was wondering if there's a way to do the same using a dictionary…
random-xyz
  • 137
  • 4
  • 14
4
votes
2 answers

How to handle exceptions in dictionary comprehension

I have two dictionaries: length = {1: 3, 2: 9, 3: 1, 4: 1, 5: 0, 6: 0, 7: 5, 8: 0} result = {1: 3.0, 2: 3.7416573867739413, 3: 7.874007874011811, 4: 6.4031242374328485, 5: 4.0, 6: 0.0, 7: 5.0, 8: 1.0} When the keys match, I need to divide the…
artemis
  • 6,857
  • 11
  • 46
  • 99
4
votes
2 answers

dictionary-comprehension with conditional calling nested list-comprehension

I have a dictionary of keys and lists. I'd like to iterate through the dictionary, get each list, iterate through each list and apply a condition, then append that filtered list to a new dictionary. The function already works imperatively. Can I do…