Note: Get the first and last items of Python OrderedDict non-destructively

The first or last item (but non-destructively) of the OrderedDict that you sometimes want. What is it that you can't index even though you remember the order?

First item

python


>> 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')

Last item

python


>> next(reversed(od))
3

The following is omitted.

The source has been forgotten somewhere on stackoverflow.

Recommended Posts

Note: Get the first and last items of Python OrderedDict non-destructively
[Python] How to get the first and last days of the month
Note: How to get the last day of the month with python (added the first day of the month)
[Python] Get the last updated date of the website
Get the last element of the array by splitting the string in Python and PHP
First Python 3 ~ The beginning of repetition ~
Get the first element of queryset
Python Note: Get the current month
Get and set the value of the dropdown menu using Python and Selenium
Get the title and delivery date of Yahoo! News in Python
Get the last day of the specified month
[Python] Get the character code of the file
Python Note: The secret role of commas
A Python beginner first tried a quick and easy analysis of weather data for the last 10 years.
Get the number of articles accessed and likes with Qiita API + Python
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the contents of git diff from python
Summary of the differences between PHP and Python
[Python] Get / edit the scale label of the figure
[Note] Export the html of the site with python.
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
A note about the python version of python virtualenv
Compare the speed of Python append and map
[Note] About the role of underscore "_" in Python
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
Get the stock price of a Japanese company with Python and make a graph
Get the title of yahoo news and analyze sentiment
Mayungo's Python Learning Note: List of stories and links
The story of Python without increment and decrement operators.
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
The process of installing Atom and getting Python running
Python --Explanation and usage summary of the top 24 packages
[python] Get the list of classes defined in the module
the zen of Python
Visualize the range of interpolation and extrapolation with python
Get the return code of the Python script from bat
Referencing and changing the upper bound of Python recursion
I checked out the versions of Blender and Python
Get the size (number of elements) of UnionFind in Python
[Python] Get the list of ExifTags names of Pillow library
[Django 2.2] Sort and get the value of the relation destination
Get the operation status of JR West with Python
Get an abstract understanding of Python modules and packages
Why is the first argument of [Python] Class self?
[Python] Get the number of views of all posted articles
Get the URL of the HTTP redirect destination in Python
Python Note: The mystery of assigning a variable to a variable
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
Try to get the function list of Python> os package
[Python] Heron's formula functionalization and calculation of the maximum area
Get the MIME type in Python and determine the file format
Get the number of specific elements in a python list