Questions tagged [python-collections]

A Python module that implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

129 questions
58
votes
11 answers

in VS Code ImportError: cannot import name 'Mapping' from 'collections'

I am trying to connect to Postgress and create a folder test.db via Flask. When I run "python3" in the terminal and from there when I run "from app import db" I get an import error: ImportError: cannot import name 'Mapping' from 'collections'…
Mitra
  • 591
  • 1
  • 3
  • 7
51
votes
2 answers

Python collections.Counter: most_common complexity

What is the complexity of the function most_common provided by the collections.Counter object in Python? More specifically, is Counter keeping some kind of sorted list while it's counting, allowing it to perform the most_common operation faster than…
Romain G
  • 1,276
  • 1
  • 15
  • 27
30
votes
6 answers

How to initialize defaultdict with keys?

I have a dictionary of lists, and it should be initialized with default keys. I guess, the code below is not good (I mean, it works, but I don't feel that it is written in the pythonic way): d = {'a' : [], 'b' : [], 'c' : []} So I want to use…
Amir
  • 1,926
  • 3
  • 23
  • 40
25
votes
1 answer

Is collections.defaultdict thread-safe?

I have not worked with threading in Python at all and asking this question as a complete stranger. I am wondering if defaultdict is thread-safe. Let me explain it: I have d = defaultdict(list) which creates a list for missing keys by default. Let's…
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
24
votes
2 answers

pandas.DataFrame.from_dict not preserving order using OrderedDict

I want to import OData XML datafeeds from the Dutch Bureau of Statistics (CBS) into our database. Using lxml and pandas I thought this should be straigtforward. By using OrderDict I want to preserve the order of the columns for readability, but…
dkapitan
  • 859
  • 2
  • 10
  • 21
22
votes
1 answer

What's the first argument of namedtuple used for?

We use namedtuple like this: >>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) >>> p=Point(1,2) >>> p.x 1 I found the first argument of namedtuple seems useless, since: Firstly, we can not use it (to create an…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
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
15
votes
6 answers

Understanding Count Triplets HackerRank

I have been working on this challenge: Count Triplets, and after a lot of hard work, my algorithm did not work out for every test case. Since in the discussion, I have seen a code and tried to find out the real functionality of the code, I am still…
Alok
  • 8,452
  • 13
  • 55
  • 93
12
votes
1 answer

Significance of Capitalization in Python collections module?

Is there any significance to the capitalization practices for classes in the collections module in Python? In particular, I find it puzzling that OrderedDict uses CamelCase while defaultdict is all lowercase. My assumption would be they would all…
willk
  • 3,727
  • 2
  • 27
  • 44
10
votes
2 answers

How to check an object has the type 'dict_items'?

In Python 3, I need to test whether my variable has the type 'dict_items', so I tried something like that : >>> d={'a':1,'b':2} >>> d.items() dict_items([('a', 1), ('b', 2)]) >>> isinstance(d.items(),dict_items) Traceback (most recent call last): …
Eric
  • 4,821
  • 6
  • 33
  • 60
10
votes
4 answers

Sort Counter by frequency, then alphabetically in Python

I am trying to use counter to sort letters by occurrence, and put any that have the same frequency into alphabetical order, but I can't get access to the Value of the dictionary that it produces. letter_count =…
iFunction
  • 1,208
  • 5
  • 21
  • 35
9
votes
1 answer

Is collections.abc.Callable bugged in Python 3.9.1?

Python 3.9 includes PEP 585 and deprecates many of the types in the typing module in favor of the ones in collections.abc, now that they support __class_getitem__. This is the case with for example Callable. To me it would seem that typing.Callable…
ruohola
  • 21,987
  • 6
  • 62
  • 97
9
votes
3 answers

namedtuple with unicode string as name

I'm having trouble assigning unicode strings as names for a namedtuple. This works: a = collections.namedtuple("test", "value") and this doesn't: b = collections.namedtuple("βαδιζόντων", "value") I get the error Traceback (most recent call last): …
Thomas
  • 249
  • 1
  • 6
8
votes
8 answers

Arrange elements with same count in alphabetical order

Python Collection Counter.most_common(n) method returns the top n elements with their counts. However, if the counts for two elements is the same, how can I return the result sorted by alphabetical order? For example: for a string like: BBBAAACCD,…
stfd1123581321
  • 163
  • 1
  • 2
  • 6
7
votes
2 answers

How to use a specific data structure as the default_factory for a defaultdict?

I am currently using a defaultdict of Counter to uniquely count several unpredictable values for unpredictable keys: from collections import defaultdict, Counter d = defaultdict(Counter) d['x']['b'] += 1 d['x']['c'] += 1 print(d) This gives me the…
WoJ
  • 27,165
  • 48
  • 180
  • 345
1
2 3
8 9