Questions tagged [defaultdict]

A subclass of Python's dict class that allows to specify a default factory to use for missing keys.

This tag would be applicable to Python questions related with the instantiation, filling and subclassing of the collections.defaultdict class.

defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class.

collections.defaultdict([default_factory[, ...]])

The first argument provides the initial value for the default_factory attribute; it defaults to None. Commonly used default_factories are int, list or dict.

>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

this is equivalent to:

>>> d = dict()
>>> for k, v in s:
...     d.setdefault(k, []).append(v)
...

http://docs.python.org/library/collections.html

647 questions
-1
votes
1 answer

Can someone explain def _trie(): return defaultdict(_trie)

Can someone explain def _trie(): return defaultdict(_trie) ? I know defaultdict and it looks like a recursive function. But I have not figured out how function name can become a parameter to defaultdict. BTW, I got this trie implementation from…
Qiulang
  • 10,295
  • 11
  • 80
  • 129
-1
votes
1 answer

Problem with collecting indexes via defaultdict

I’m new to coding and have a problem with my homework. I need to create function which would take some collection as an argument and return dictionary where each element of the collection would be the key and index of element would be the value. I…
Ronin2903
  • 3
  • 1
-1
votes
1 answer

How to create dictionary with list with regex and defaultdict

A dictionary is below my = [{'Name':'Super', 'Gender':'Male', 'UNNO':111234}, {'Name':'Spider', 'Gender':'Male', 'UNNO':11123}, {'Name':'Bat', 'Gender':'Female', 'UNNO':113456}, {'Name':'pand', 'Gender':'Female', 'UNNO':13456}] The…
user11727742
-1
votes
1 answer

python defaultdict and increment/aggregate a value

I am trying to create a dictionary in which i have to keep incrementing a value So i am trying the following: from collections import defaultdict test = defaultdict(dict) for item in debitlist: if not something: …
Santhosh
  • 9,965
  • 20
  • 103
  • 243
-1
votes
1 answer

conditions on undefined keys in defaultdict

I am new to python I wanna write a code using defaultdict in collections module in which , something like this: defaultdict (lambda:'0') but I want the values of only those undefined keys to 0 for which key is greater than 0 like for eg: I have…
-1
votes
1 answer

default dict in grouping

My code doesn't give me the same results as my assert function doesn't work. What might be the problem? I've tried shifting the 'return d' function one tab right to shorten what the code might do and after printing 'groups1' and 'test_max_item',…
-1
votes
1 answer

Unhashable type: 'list' error in values in dict

I have two new dictionaries as follows: a = {Banana: 60, Apple: 90, Pear: 80} b = {60: 25, 90: 45, 89: 94} I wanted to create a new dictionary that only took the value in dictionary b and then merged it into a. So like Banana has a value of 60,…
-1
votes
1 answer

Get value that from another defaultdict and update the original dict

Basically, I am trying to extract the values from one dictionary and update the value in another dictionary. I have four lists as follows: a = [1,1,2,3,4,5] b = [0,3,0,5,6,0] c = [2,3,4,5,6,5] d = [20,30,40,50,60,70] So I use a defaultdict to store…
-1
votes
1 answer

How do I parse a default dict with values that are lists into independent dicts

Trying to parse a defaultdict(list) Input: defaultdict(class <'list'>,{'key1': ['v1', 'v2'], 'key2': [v3, v4], 'key3': ['v5', 'v6']}) desired output: list_dic = [{key1: v1, key2: v3, key3: v5}, {key1: v2, key2: v4, key3: v6}] Has to work for n…
-1
votes
3 answers

Python defaultdict reference

Actually, I don't know how to explain this question in a proper title. Any edition is welcome. Let's just see the example. # python 2.7.x import collections d = collections.defaultdict(int) d['a'] = 2 d['b'] = 1 res = [d]* 2 res[0]['a'] -= 1 print…
rj487
  • 4,476
  • 6
  • 47
  • 88
-1
votes
1 answer

How to get values from a defaultdict using a new combination of keys?

I have the following defaultdict: dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}} From this dict, I need to append the values to a list using the following combination of keys: order = [("b","b"), ("b","a"), ("b","c"), ("a","b", ("a","a"),…
-1
votes
2 answers

How to increment a value (in defaultdict of defaultdicts)?

How to increment d['a']['b']['c'][1][2][3] if d is defaultdict of defaultdict without code dublication? from collections import defaultdict nested_dict_type = lambda: defaultdict(nested_dict_type) nested_dict = nested_dict_type() #…
Pleeea
  • 362
  • 4
  • 12
-1
votes
3 answers

Python : Default Dictionary Initialization

I have created a default dictionary as below : from collections import defaultdict dd = defaultdict(lambda : "Key not found") dd = {'a':1,'b':2} print(dd) print(dd['a']) # Prints 1 print(dd['c']) # Throws KeyError But, the below code snippet…
Shraddha
  • 791
  • 7
  • 13
-1
votes
1 answer

Python dictionary with a tuple and tuple count as the value

I have a .csv file: csv file containing packet header data from a wireshark scan that I am iterating through line by line with a for loop. The list contains around 100,000 items, many of which are repeated. I am trying to find how many times each…
Kyle Wollman
  • 99
  • 1
  • 7
-1
votes
1 answer

How to insert values at specific index in defaultdict in Python3

i'm a beginner on Python and what i'm trying to do is adding an element in a specific index in a list of values associated to my key in Python 3, using defaultdict. My default_dict structure is the following: default_dict = ['label_1':[list of…
Basionkler
  • 13
  • 7