Questions tagged [setdefault]

49 questions
238
votes
18 answers

Use cases for the 'setdefault' dict method

The addition of collections.defaultdict in Python 2.5 greatly reduced the need for dict's setdefault method. This question is for our collective education: What is setdefault still useful for, today in Python 2.6/2.7? What popular use cases of…
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
78
votes
8 answers

python dict: get vs setdefault

The following two expressions seem equivalent to me. Which one is preferable? data = [('a', 1), ('b', 1), ('b', 2)] d1 = {} d2 = {} for key, val in data: # variant 1) d1[key] = d1.get(key, []) + [val] # variant 2) …
Cerno
  • 783
  • 1
  • 5
  • 5
41
votes
4 answers

How to set a hidden value in Razor

I know that what I'm trying to do is bad idea, but I have specific constrains for now. I have multiple sites, using one and the same MVC3 code base. For one of them the requirement is to hide some required fields from the form. I know that the best…
Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
19
votes
1 answer

Is the defaultdict in Python's collections module really faster than using setdefault?

I've seen other Python programmers use defaultdict from the collections module for the following use case: from collections import defaultdict s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] def main(): d =…
damzam
  • 1,921
  • 15
  • 18
16
votes
3 answers

setdefault vs defaultdict performance

I am writing code for an application where performance is important. I am wondering why defaultdict seems to be faster then setdefault. I would like to be able to use setdefault, mostly because i do not like the print output of the nested…
snowleopard
  • 717
  • 8
  • 19
7
votes
1 answer

Why does this python dictionary get created out of order using setdefault()?

I'm just starting to play around with Python (VBA background). Why does this dictionary get created out of order? Shouldn't it be a:1, b:2...etc.? class Card: def county(self): c = 0 l = 0 groupL = {} # groupL for Loop for n in…
DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
5
votes
1 answer

Why does setdefault evaluate default when key is set?

To illustrate the problem I created a simple example: #!/usr/bin/env python class Person(): def __init__(self): self.cache = {} def get_person_age(self): def get_age(): print "Calculating age..." …
Andrei
  • 117
  • 8
4
votes
1 answer

C++ (STL map) equivalent method to Python's setdefault

So most likely this question had been asked already. Couldn't find it. Every time I define a std::map and want to insert some value to it, I use this piece of code: using IntVector = vector < int > ; map mapTmp; int iKey = 7; int…
idanshmu
  • 5,061
  • 6
  • 46
  • 92
3
votes
2 answers

How do I create dictionary keys from list of lists?

List of strings: ['Georgie Porgie', 87, '$$$', ['Canadian', 'Pub Food'], 'Queen St. Cafe', 82, '$', ['Malaysian', 'Thai'], 'Dumplings R Us', 71, '$', 'Chinese', 'Mexican Grill', 85, '$$', 'Mexican', 'Deep Fried Everything', 52, '$', 'Pub Food'] I…
Yogesh Riyat
  • 129
  • 1
  • 7
3
votes
1 answer

How use set_default in foreign key in Room Android

I don't understand how use set_default in a foreign key in onDelete. Where do I set a default value for child row? Please tell me, what can I do?
3
votes
2 answers

setdefault() takes no keyword arguments

I have the following code snippet: mirna2age = {} for i in agesdb: mirna2age.setdefault(i[0],default=[]).append(i[1]) However, Python returns TypeError: setdefault() takes no keyword arguments I am unsure why. Does anyone have any ideas?
indiaash524
  • 167
  • 1
  • 9
2
votes
2 answers

Is it possible to have nested dictionary using setdefault, appending to a list?

I would like to have a nested dictionary with a list of values that are attached to the sub-key. Can't get the sub-key to recognise. month = {} for row in date: month.setdefault(row[1],{row[0]: []}) …
Susan
  • 51
  • 1
  • 10
2
votes
1 answer

What's the difference between default and set default in Postgres?

I am kind of new to Postgres and I'm trying to use the following query in Postgres: alter table tablename add column col1 enum('r', 'g', 'b') not null set default 'r'; It is giving me an error. When I changed it to default instead of set default,…
Raj
  • 3,637
  • 8
  • 29
  • 52
2
votes
2 answers

dict.setdefault appends one extra (default?) item into the value list

I am using a dictionary to group data from a CSV file, so for instance fist and second columns are the dict key and value will be a list of tuples with column 3,4. my code snippet is: import csv import collections csvDicData_ = dict() fh =…
Alg_D
  • 2,242
  • 6
  • 31
  • 63
2
votes
3 answers

Python dict.setdefault uses more memory?

I was writing some Python code that involved something like this values = {} for element in iterable: values.setdefault(element.name, []).append(element) Because I could have sorted the input previously, I also implemented it like this values =…
Tianyang Li
  • 1,755
  • 5
  • 26
  • 42
1
2 3 4