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
-1
votes
3 answers

Append elements in the value field of a dictionary using comprehensions

I have a list of elements, lets say: y = [1, 3, 1, 5, 1] And I would like to create a dictionary where: Keys: are the elements in y Values: is a list of the elements that appear before the Key in y I attempted the following…
-1
votes
1 answer

Insert conditional values into dict from key using RE

So my problem is quite simple, my conditions are: if "__" in key, insert 'class=\"\" ' into opening tag in value pair: so it becomes: then insert the relevant values of "__" else leave as is I've done…
-1
votes
5 answers

get same value for keys into another dictionary from List of Dictionary

Here is my list of dictionary. d = [{'count': '100', 'zone': 'PIMPRI', 'cycle': '15'}, {'count': '50', 'zone': 'PIMPRI', 'cycle': '15'}, {'count': '150', 'zone': 'PIMPRI', 'cycle': '30'}] How can I get output like below: d_one =…
Luffy
  • 83
  • 1
  • 10
-1
votes
1 answer

Get list with more elements in dictionary of lists

I've already seen several posts about dictionary of lists but none of them could help me so far. I have a dictionary of lists like: dict = {'a': [1,5,4], 'b': [4], 'c': [1,5,4,3,8], 'd': [1,4]} Now I want, in a loop, get the list with more…
digs_ef
  • 11
  • 3
-1
votes
3 answers

list to dictionary comprehensions

I'm practicing some python syntax exercises and I decided to write a dictionary comprehension based on a similarly designed list comprehension. But while the later is OK, the former results in a syntax error. This is my list comprehension.. >>> l =…
Paperclip Bob
  • 346
  • 2
  • 8
-1
votes
1 answer

build a complicated dict by using dict comprehension

Here is a question for python programming. I am trying to build a dict by importing data from excel for analyzing. my structure of the dictionary is: dict = { sheet_name : {label_name1 : [['datatype'],['','',some_data.....]], …
El'tar
  • 1
  • 1
-1
votes
6 answers

Python list comprehension with dicts and filters

I have a list like this one: l1 = [{'id':'78798798','gender':'Male'}, {'id':'78722228','gender':'Female'}, {'id':'33338','gender':'Male'}] I need to check the length of a list obtained using a list comprehension and filtered by…
-1
votes
1 answer

Filtering defaultdict with dictionary comprehension

I have a defaultdict where keys are a number and values are lists with two entries each. I want to filter by a condition based on the first entry. I tried using the suggestion here: filter items in a python dictionary where keys contain a…
-1
votes
1 answer

Understanding counter and dictionary comprehensions in python

I have tried to find an explanation but couldn't, so my apologies if this is a silly question. Having data x: x = [(1, {'gender': 'male'}), (2, {'gender': 'female'}), (3, {'gender': 'male'}), (4, {'gender': 'female'}), (5,…
-1
votes
4 answers

How can I insert a dictionary that points to multiple dictionaries inside a list

can anyone please help me with the following code: [{k: {x: y for x, y in b.items()} for b in arg for k in range(len(arg))}] Where arg is: arg = [{"key1": "val1", "key2": "val2"}, {"key1": "val3", "key2": "val4"}] The output Im looking for is: [0:…
-1
votes
1 answer

Create dictionary comprehension from list with condition syntax

I'd like to create a dictionary using dictionary comprehension syntax. Note that list l contains tuples of strings and tuples with 1st element always a time stamp. This works: d = {} for entry in l: if entry[0] not in d: d[entry[0]] =…
ptay
  • 736
  • 6
  • 11
-1
votes
1 answer

constructing a nested dictionary using dict comprehension

Getting frustrated here so help very welcome. I have a file, data_fields lname,fname,age,UID macpherson,tom,16,219 goldman,tim,13,316 smith,jon,11,414 doe,jon,59,512 I am making a dictionary keyed on the 'UID' value as below import csv with…
user5799671
  • 223
  • 2
  • 8
-1
votes
3 answers

Compare the key/values of two dictionaries and put into new dictionary

I know variations on this question already exist, but I cannot find one that matches exactly what I am trying to achieve. I have the following code, which included a solution I have taken from a solution to a similar question: b =…
gdogg371
  • 3,879
  • 14
  • 63
  • 107
-1
votes
1 answer

Dictionary comprehension to avoid lines of code

Hi want to understand how to make this code shorter using dictionary comprehension: for e in list_of_tuples: tmp = mydict.copy() tmp[e[0]] = tmp[e[1]] if someFunction(tmp): mydict = tmp I…
linello
  • 8,451
  • 18
  • 63
  • 109
-1
votes
1 answer

Python single statement dictionary generator

Is there a way in Python to do new_list = [x for x in items] (list comprehension) for a dictionary? Something like new_dict = [x=>y for x, y in items.iteritems()] so that I get {x: y}
A23
  • 1,596
  • 2
  • 15
  • 31