Skip to content

Instantly share code, notes, and snippets.

@r-plus
Created November 23, 2012 03:29
Show Gist options
  • Select an option

  • Save r-plus/4133883 to your computer and use it in GitHub Desktop.

Select an option

Save r-plus/4133883 to your computer and use it in GitHub Desktop.
get first and last key from dictionary instance.
import datetime
day23 = datetime.datetime(2012, 10, 23)
day24 = datetime.datetime(2012, 10, 24)
# dictionary does not have order.
dict = {day24 : 24, day23 : 23}
# get first key
sorted(dict.keys())[0]
# get last key
sorted(dict.keys())[len(dict.keys()) - 1]
@Sakurina
Copy link
Copy Markdown

I don't know if you know this but using [-1] on line 11 would be equivalent to [len(dict.keys()) - 1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment