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

How to give unique integer id starting from 0 to random given number?

What I want to implement in Python: # 1. define int_factory # 2. get new id of value 123 --> give 0 to 123 a = int_factory[123] print(a) # = 0 # 3. get new id of value 12324 --> give 1 to 12324 a = int_factory[12324] print(a) # = 1 # 4. Hey,…
user3595632
  • 5,380
  • 10
  • 55
  • 111
-2
votes
1 answer

from collections import defaultdict

why is it when I do not set default value of defaultdict to be zero (int), my below program does not give me results: >>> doc 'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my…
yasnil
  • 77
  • 8
-2
votes
1 answer

How do I access the individual indexes of a list of named tuples within a defaultdict?

I have created a defaultdict using a csv file. The defaultdict has a movie director as the key and a list of named tuples as the values. The named tuples have 3 elements: title, year and score. I need to isolate the year element, check whether the…
balter
  • 25
  • 1
  • 9
-2
votes
1 answer

exec and defaultdict assignment in python3

I'm trying to build a function that assign keys and values to a defaultdict in python3, but it fails in exec execution. Let's say that I need to pass several str variables as keys and other as values in a defaultdict inside: Define a…
cccnrc
  • 1,195
  • 11
  • 27
-2
votes
2 answers

python: remove type from defaultdict

i have the following code: #!/usr/bin/python farm_sub_count = [[['Farm', u'Red Hat Enterprise Linux for Virtual Datacenters with Smart Management, Premium'], 2], [['Farm', u'Red Hat Enterprise Linux for Virtual Datacenters with Smart Management,…
askpython
  • 75
  • 2
  • 8
-2
votes
2 answers

Rename defaultdict key in Python

I have the following problem: I have a defaultdict called word_count containing words and the number how often they occur. I get this by counting the reply of the Google Speech API. However, this API gives me back things like '\303\266' for the…
Ben
  • 3
  • 5
-2
votes
1 answer

Compare two defaultdictionaries Python

Is there a more efficient way to compare two dictionaries than a double loop ? for i in d: for i2 in d2: if i == i2: key1 = d.get(i) key2 = d2.get(i2) print("First key:", key1) …
Pickeroll
  • 908
  • 2
  • 10
  • 25
-2
votes
3 answers

How to find the maximum value for each key in a dictionary of lists?

How can I extract the maximum value for each key in a dictionary of lists? For example #Generate some sample data s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] d = defaultdict(list) for k, v in s: d[k].append(v) >>>…
Borealis
  • 8,044
  • 17
  • 64
  • 112
-2
votes
1 answer

Dictionary Formatting and defaultdict function

I have a dictionary which is the format of {(a,b):c, (a2,b2):c2 and so on}. From this format, there are more than one key of a2, a and so on and for each a, a2 the b, b2 however occurs only once and the value c,c2 for each item varies. What i need…
-3
votes
3 answers

Why is this code taking longer time than expected?

Question: Given a list of unsorted elements, we have to find the length of longest consecutive elements sequence. Expected time complexity: O(N) for Ex: [4,7,1,100,28,2,3] output: 4 since the longest consecutive elements sequence is [1,2,3,4] from…
-3
votes
2 answers

How to extract values from defaultdict with nested list values?

I have a defaultdict with nested list values and I need to extract the values as shown in the output. dd = defaultdict(list) print(dd) Input: defaultdict(, { '1': [[['Peter', '100'], ['John', '200'], ['Carlos', '150'], ['Rick',…
-3
votes
3 answers

Getting values out of a default dict

I have a defaultdict of shape: defaultdict(.>, {1: defaultdict(set, {'A': {0, 1}}), 2: defaultdict(set, {'A': {1, 3}, 'E': {12, 14}}), 3: defaultdict(set, …
Qubix
  • 4,161
  • 7
  • 36
  • 73
-3
votes
2 answers

Python DefaultDict Ordering Multiple Values Issue

I am trying to order all of the scores in order of each user's score from highest to lowest. I have attempted this with the code below: import collections from collections import defaultdict from operator import itemgetter worker_scores =…
Delbert J. Nava
  • 121
  • 1
  • 9
-4
votes
1 answer

defaultdict key default value issues

I'm new to Python and having an issue with defaultdict. I have some json, where the lastInspection key isn't always present. I need to put a default value in for the date. p.get("lastInspection", "") returns me {'date': '2018-01-03'} problem =…
-4
votes
1 answer

PEP 8 warning "Do not use a lambda expression use a def" for a defaultdict lambda expression

I am using the below python code to create a dictionary.But I am getting one PEP 8 warning for the dct_structure variable. Warning is: do not use a lambda expression use a def from collections import defaultdict dct_structure = lambda:…
Arijit Panda
  • 1,581
  • 2
  • 17
  • 36
1 2 3
43
44