3
3

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 5 years have passed since last update.

備忘録: Python OrderedDict の先頭と最後のアイテムを非破壊的に取得する

Last updated at Posted at 2015-10-28

たまに欲しくなる OrderedDict の先頭か最後のアイテム (ただし非破壊的に)。
順番覚えてるくせにインデクシングできないって何なん?的な。

#先頭のアイテム

>> from collections import OrderedDict

>> od = OrderedDict(((1, 'a'), (2, 'b'), (3, 'c')))

>> od
OrderedDict([(1, 'a'), (2, 'b'), (3, 'c')])

>> next(iter(od))
1

>> next(iter(od.values()))
'a'

>> next(iter(od.items()))
(1, 'a')

#最後のアイテム

>> next(reversed(od))
3

以下略。

ソースは stackoverflow のどこかわすれました。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?