Questions tagged [ordereddict]

Python OrderedDict datatype, which remembers the order entries were added to the dictionary.

Python OrderedDict datatype, which remembers the order entries were added to the dictionary. Also see collections — High-performance container datatypes in the Python manual.

77 questions
100
votes
1 answer

Will OrderedDict become redundant in Python 3.7?

From the Python 3.7 changelog: the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec. Would this mean that OrderedDict will become redundant? The only use I can think of it…
James Hiew
  • 3,514
  • 4
  • 21
  • 33
98
votes
11 answers

How to add an element to the beginning of an OrderedDict?

I have this: d1 = OrderedDict([('a', '1'), ('b', '2')]) If I do this: d1.update({'c':'3'}) Then I get this: OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]) but I want this: [('c', '3'), ('a', '1'), ('b', '2')] without creating new dictionary.
user2392209
  • 1,011
  • 1
  • 8
  • 8
39
votes
5 answers

Iterate over OrderedDict in Python

I have the following OrderedDict: OrderedDict([('r', 1), ('s', 1), ('a', 1), ('n', 1), ('y', 1)]) This actually presents a frequency of a letter in a word. In the first step - I would take the last two elements to create a union tuple like this; …
Dejell
  • 12,840
  • 37
  • 129
  • 217
5
votes
1 answer

Trying to code a complex algorithm with `OrderedDict` data Structure

I am trying to write an algorithm: i have this input of OrderedDict data type like the following: odict_items([(3, [(0, 1), (1, 1), (1, 1)]), (11, [(0, 0), (1, 1), (1, 1)]), (12, [(0, 0), (1, 1), (1, 1)])]) I am trying to write a function to added…
Catalina
  • 736
  • 1
  • 11
4
votes
3 answers

Python convert OrderedDict saved as string into an actual dict

I have a Postgres db where OrderedDict has been saved as a string. I need to convert this string into json/dict so that it can be saved in a JSONField. How can I convert this string into dict? String example - OrderedDict([('order_id', 'xxxxxx'),…
Garvit Jain
  • 397
  • 5
  • 22
4
votes
4 answers

How to step through a large ordered dictionary in Python 3.7?

Lately I've been refactoring some bash scripts into Python 3.7 as both a learning exercise and for real use in a project. The resulting implementation uses a very large ordered dictionary, say around 2 to 3 million entries. Storing the data this…
3
votes
3 answers

How can I do to convert OrderedDict to Dict

I have a this list a = [OrderedDict([('a','b'), ('c','d'), ('e', OrderedDict([('a','b'), ('c','d') ]))])] and I want to convert the OrderedDict in dictionary. Do you know how could I do ? Thank you !
3
votes
1 answer

How should I test that dictionaries will always be in the same order?

This is a testing question, not an implementation question. I have a program which produces a JSON, I want to be able to compare outputs consistently, so I'm converting all of my dicts to OrderedDicts. I've searched through, and I'm pretty…
Brian Postow
  • 10,227
  • 14
  • 69
  • 113
2
votes
1 answer

Memory leak free OrderedDict alternative in Python 2.7?

We are checking whether one of our packages leaves garbage objects in the Python gc and found that collections.OrderedDict objects are not deleted by the ref-count mechanism but are put into the GC, with the following references (when pprint'ing…
Andreas Maier
  • 2,129
  • 1
  • 20
  • 24
2
votes
1 answer

How to extract data from salesforce ordereddict hierarchy using Python & Pandas

Summary In short, I need to extract data from a pandas series containing individual OrderedDicts. So far progress has been good but I have now hit a stumbling block. When I define my own dataframe for demonstration purposes on Stack Overflow I'm…
Ryan Davies
  • 167
  • 1
  • 9
2
votes
2 answers

Extracting Data from OrderedDict

So i have a firebase database and this is my code to get specific data db = firebase.database() test = db.child("Users").order_by_child("IDNumber").equal_to(222333123).get().val() print(test) then the result returns as an…
2
votes
2 answers

Is it ok to pass an OrderedDict as a Celery task argument?

I have inside a Django REST Framework's serializer an overridden update method. In this update, as user can send lots of children, I have an asynchronous Celery task process_children, to deal with the kids. class…
Sifnos
  • 991
  • 1
  • 10
  • 21
2
votes
2 answers

Python group values in 2d list from CSV

I have the following…
Matt Price
  • 33,201
  • 6
  • 21
  • 33
2
votes
1 answer

Dump OrderedDict in a more concise way

I have a YAML document in a file that I need to update with some structured information, that I get from a library. The order in which keys from this information are dumped is important. The YAML file (input.yaml) looks like: %YAML 1.1 --- - element…
Anthon
  • 51,019
  • 25
  • 150
  • 211
1
vote
1 answer

Pop element at the beginning or at the last from dict in python3

Collection module in python has a datatype OrderedDict(), which enables us to save the key value pairs in the same order as they were inserted in dict. It has a method popitem, which allows us to pop items at the beginning or at the…
1
2 3 4 5 6