1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

collections.OrderedDict

Posted at

#OrderedDict順番を覚えています

import collections

d = {'apple': 4, 'banana': 3, 'pear': 1, 'orange': 2}

# アルファベット順で並び替える、値の順番で並び替えたい場合はt[1]とする
od = collections.OrderedDict(sorted(d.items(), key=lambda t: t[0]))
print(od)

# 最後に追加されます
od['cc'] = 100
print(od)

実行結果:

OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1), ('cc', 100)])
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?