Questions tagged [dict-comprehension]

13 questions
47
votes
6 answers

Dict merge in a dict comprehension

In python 3.5, we can merge dicts by using double-splat unpacking >>> d1 = {1: 'one', 2: 'two'} >>> d2 = {3: 'three'} >>> {**d1, **d2} {1: 'one', 2: 'two', 3: 'three'} Cool. It doesn't seem to generalise to dynamic use cases, though: >>> ds = [d1,…
wim
  • 338,267
  • 99
  • 616
  • 750
3
votes
1 answer

if-else in a dictionary comprehension

Is it possible to use the else statement (and if yes, how?) in a dictcomp? It is not possible to use else as part of the comprehension itself (see this) but at least in list and set comprehensions it is possible to use the conditional_expression…
LittleByBlue
  • 436
  • 9
  • 18
2
votes
1 answer

short for python dictionary

I tried a few searches but I didn't really know how to ask. I understand short form for loops but this portion within a dictionary is confusing me. resistances = {k: v if random.random() > self.mutProb else not v for k, v in…
Loz
  • 21
  • 1
  • 3
2
votes
1 answer

Multiply key of the same dict

I'm using python 3.5, I need to multiply this dict and this should output the result of multiplication of each key of dict. {0: [0.0008726003490401396, 0.004363001745200698, 0.0008726003490401396, 0.0008726003490401396, 0.0017452006980802793,…
1
vote
2 answers

Squeeze in an entry to a dictionary while creating the latter with enumerate function

Is there a way of writing the following on a single line? x = {item: i for i, item in enumerate([letters for letters in ascii_lowercase])} x[' '] = 27 I tried something like x = {item: i for i, item in enumerate([letters for letters in…
1
vote
1 answer

Condensing the following code, preferably using a dictionary comprehension

Is it possible (and, more importantly, practical) to write the following code as a dict comprehension? I am creating a dictionary here and then checking for "blank" values (represented by a '-') and replacing it with another string value. test_dict…
1
vote
1 answer

Nested dictionary comprehension to avoid empty values

I have an api call that returns a dictionary with all the ids for all users and others to get the userDetails like: >>> allIds=api.getAllIds() {1,2,3,4,5} >>> userDetails=api.getUserDetails(1) {'name':'Bob','age':'19'} I'm trying to pack the whole…
1
vote
2 answers

Python dictionary comprehension with Pandas

I am trying to create a dictionary from two columns of a DataFrame (df) mydict={x :y for x in df['Names'] for y in df['Births']} But all of the values are the same(the last value in the column)! {'Bob': 973, 'Jessica': 973, 'John': 973, 'Mary':…
AboJoe
  • 43
  • 1
  • 4
1
vote
3 answers

How to join two lists of dictionaries in Python?

I have the following simple data structures: teams = [ { 'league_id': 1, 'name': 'Kings' }, { 'league_id': 1, 'name': 'Sharkls' }, { 'league_id': 2, 'name': 'Reign' }, { 'league_id': 2, 'name': 'Heat' } ] leagues = [ { 'league_id': 1, 'name':…
Wells
  • 10,415
  • 14
  • 55
  • 85
1
vote
1 answer

Making a nested dictionary with lists and arrays

I have two lists and an array: owners = [ 'Bill', 'Ann', 'Sarah'] dog = ['shepherd', 'collie', 'poodle', 'terrier'] totals = [[5, 15, 3, 20],[3,2,16,16],[20,35,1,2]] I want to make a nested dictionary out of these. dict1 = {'Bill': {'shepherd':…
Caroline.py
  • 293
  • 1
  • 4
  • 16
0
votes
2 answers

Python: How to create nested dictionaries out of lists with specific values

I apologize in advance for the long post, but I've made sure it's easy to follow and very clear. My question is this: How can I create a nested dictionary out of lists, with specified duplicate keys? Here's an example of what I'd like to make,…
-1
votes
1 answer

How do you build a nested dict comprehension that imitates 'records' made by '.to_dict'?

I have read a CSV file into Pandas and converted the resulting dataframe into a list of dictionaries for each row using the 'to_dict(orient='records') function. A shortened version of the list looks like this: records = [{'addjob': 'ADDJOB', …
-5
votes
3 answers

Python - convert list to dict

I have the following list: l = [('Alice',12),('Bob',10),('Celine',11)] I want to get the following dict (as correctly pointed in a comment below, this is not a dict. In reality, I just want a list of dicts): [ {'name':'Alice','age':12}, …
Nik
  • 5,515
  • 14
  • 49
  • 75