I want to sort a dictionary with key being month-year

python sort dictionary by datetime value
python sort list of dictionaries by date
python json sort keys alphabetically
python sort by datetime

I want to sort a dictionary with key being month-year.

Eg: For

March 2017,January 2018,May 2017 etc.

My dictionary keys should be sorted in this order:

March 2017,May 2017,January 2018

What to do ?


{'February 2017': {'a': 1.0, 'b': 683.01}, 
 'March 2018': {'a': 0.0, 'b': 623.79}, 
 'March 2017': {'a': 1.0, 'b': 683.01}}

NB: Values of Month Year comes dynamically.

You need to convert the keys into dates so they will sort properly. Assuming your dictionary is called example_dict

import datetime
keys = list(example_dict.keys())
keys.sort(key=lambda x: datetime.datetime.strptime(x,'%B %Y'))

python - I want to sort a dictionary with key being month-year, I want to sort a dictionary with key being month-year. Eg: For. March 2017,​January 2018,May 2017 etc. My dictionary keys should be sorted in  Well, it is actually possible to do a "sort by dictionary values". Recently I had to do that in a Code Golf (Stack Overflow question Code golf: Word frequency chart ). Abridged, the problem was of the kind: given a text, count how often each word is encountered and display a list of the top words, sorted by decreasing frequency.

You need to use special key function for sorting:

months = ['January',

month_num = dict(zip(months, range(12)))

def compare(date):
    month, year = date.split(' ')
    return (year, month_num[month])

dates = {
    'February 2017': {'a': 1.0, 'b': 683.01},
    'March 2018': {'a': 0.0, 'b': 623.79}}

dict(sorted(dates.items(), key=lambda x: compare(x[0])))

Python, Given a list of dictionary, the task is to sort the dictionary by date. Let's see a ini_list.sort(key = lambda x: datetime.strptime(x[ 'd.o.b' ], '%Y-%m-%d' )). # printing​  Word of the Year. Our Word of the Year choice serves as a symbol of each year’s most meaningful events and lookup trends. It is an opportunity for us to reflect on the language and ideas that represented each year. This year’s Word of the Year, however, is extra special: 2019 is not only the end of the decade, but it also marks 10 years of

FYI: In python-2.7, since dictionaries are 'unordered', so to speak, your dictionary in the original post is syntactically incorrect, since there are two keys with the same value. The interpreter will override one of them.

I presumed that one of them should be May 2018, as per your original post, so that is the dictionary I used for my answer:

{'February 2017': {'a': 1.0, 'b': 683.01}, 
 'May 2018': {'a': 0.0, 'b': 623.79}, 
 'March 2018': {'a': 1.0, 'b': 683.01}}

Since, as stated above, dictionaries are not ordered obviously (hash values), so we must convert to a list and then play around. A one liner solution is as follows:

sorted(list(data.items()), key=lambda x: [x[0].split()[-1], x[0].split()[0], x[1]])

You get the output as follows:

>>> sorted(list(data.items()), key=lambda x: [x[0].split()[-1], x[0].split()[0], x[1]])
[('February 2017', {'a': 1.0, 'b': 683.01}), ('March 2018', {'a': 1.0, 'b': 683.01}), ('May 2018', {'a': 0.0, 'b': 623.79})]

How to sort a dictionary in Python, First, we need to know how to retrieve data from a dictionary to be passed on to Key (optional): A function (or criteria) based on which we would like to sort the  The key argument (not to be confused with the dictionary's keys) for sorted allows us to define specific functions to use when sorting the items, as an iterator (in our dict object). In both examples above the keys and values were both the items to sort and the items used for comparison, but if we want to sort our dict keys using our dict values , then we would tell sorted to do that via its key argument.

English Guide Book VYAPAM/MPPSC MADHYA PRADESH PUBLIC SERVICE , next month/year the following month/year the next month/year a month/year later last and object Case 1 I will say to my friends, “I have started learning computer​. is the word order of a statement: Subject + Verb + Object “What are you saying​? Ans: (d) I said to my friend, “Can I borrow your dictionary Direct & Indirect  A dictionary comprehension takes the form {key: value for (key, value) in iterable}. This syntax was introduced in Python 3 and backported as far as Python 2.7, so you should be able to use it regardless of which version of Python you have installed. A canonical example is taking two lists

Dictionary of the Bible , This accounts for its scantiness and occasional want of continuity. with a special name, the rest 1 being called according to their order. 1 The later Jews had two beginnings to I the year, the seventh month of the civil reckoning being Abib. df['month_year'] = df['date_column'].dt.to_period('M') You could also use D for Day, 2M for 2 Months etc. for different sampling intervals, and in case one has time series data with time stamp, we can go for granular sampling intervals such as 45Min for 45 min, 15Min for 15 min sampling etc.

Johnson's English Dictionary, as Improved by Todd, and Abridged by , son's Dictionary, are, logomachy, mononachy, sciomachy, and theomachy ; the two that sciomachy ought to be written skiamachy, I have only to observe, at present, S state of being monstrous, or out of the common order of the universe​. of former times, when persons directed in their wills, that, within a year, a month,  Finding the right instance with my_dict[key] or key in my_dict (or item in my_set) needs to perform as many equality checks as there are instances of stupidlist3 in the dict's keys (in the worst case). At this point, the purpose of the dictionary - O(1) lookup - is completely defeated.

  • Use python3.7, or use collections.OrderedDict
  • @ukemi : In that question, all key values can easily be sorted. In my question,the values come dynamically and I have to sort it. Eg: One value will be in March 2017 and other will be in March 2018.How can I sort those ?
  • @RafaelC : It works.. Thanks mate..
  • stackoverflow.com/questions/44861179/…
  • Note that you can't store different values with the same key in python dict.