sivarajesh
sivarajesh

Reputation: 13

What is the difference between dict, collections.defaultdict, collections.OrderedDict, collectinos.UserDict?

What is the difference between dict, collections.defaultdict, collections.OrderedDict, collectinos.UserDict?

Please help me with examples.

Upvotes: 1

Views: 1447

Answers (1)

Eric L.
Eric L.

Reputation: 89

They're basically all dictionnary with different flavor. You can use them as you would with dictionary.

  • dict is the "normal" one which do {key:value} without ordering or memory
  • defaultdict adds the "default" value when a key is called if the key is missing (adding the value at the same time).
  • OrderedDict remember the order in which the keys have been added.

Unfortunatly, I don't know about the last one, but here is the reference

the UserDict.data would be what act as the normal dict, but I don't know which use case would be interested in this...

Let me know if this clarify what you wanted to know. I suggest you see "super considered super!" by Raymond hettinger if you want to see something cool to do with this.

Upvotes: 1

Related Questions