1

I have the following dicti_1:

{'2017-09-01': ['PRUEBAPRUEBA', 'PRUEBA123123'], 
 '2017-10-03': ['PRUEBAPRUEBA', 'PRUEBA123123'], 
 '2017-11-08': ['PRUEBAPRUEBA', 'PRUEBA123123'], 
 '2017-12-03': ['PRUEBA123123']}

I am looking forward to check the values that appear in the latest key (as it is a date):

In order to check the latest value that corresponds to the latest key what I did was :

EDIT: From @COLDSPEED input I sorted the dictionary , I used @Devin Jeanpierre 's answer in the following link in order to sort the dictionary using the operator module: How do I sort a dictionary by value?

sorted_dict = sorted(dicti_1.items(), key=operator.itemgetter(0))

latest_key=list(sorted_dict.keys())[-1]

return sorted_dict[latest_key]

After this I am looking forward to create a dictionary with the keys of the latest date and the values that appears:

return {latest_key:sorted_dict[latest_key]}

output:

{'2017-12-03': ['PRUEBA123123']}

However in my particular case, there is one latest value the 2017-12-03 which corresponds to PRUEBA123123 and a different value PRUEBAPRUEBA with its latest date 2017-11-08 . Therefore my desired output would be something like this:

new_dicti=

{'2017-12-03': ['PRUEBA123123'], '2017-11-08': ['PRUEBAPRUEBA']}

The problem I am facing is how to design new_dict with the latest date for every distinct value

Your help is highly appreciated.

JamesHudson81
  • 2,215
  • 4
  • 23
  • 42
  • Why don't the other dates also show? It would seem `'PRUEBA123123'` is a part of those values too. – cs95 Jan 10 '18 at 09:13
  • What did you try? Do you want to show the latest date for every distinct value that appears somewhere in your dict? I'm not sure if I understood you correctly – Banana Jan 10 '18 at 09:14
  • Also, `latest_key` happens to actually be the latest key by fluke. Remember that dicts are not ordered in versions older than 3.6, so the most idiomatic way of getting the last key would be `max(dicti_1)` since dates can be compared lexicographically. – cs95 Jan 10 '18 at 09:14
  • @Banana you are right the latest date for every distinct value – JamesHudson81 Jan 10 '18 at 09:15
  • @COLDSPEED you are absolutely right , thank you for the input – JamesHudson81 Jan 10 '18 at 09:16
  • Suggestion: make a flattened list of the values in the dict (with each one only appearing once). Go through this and search the latest date s.t. the current element is contained in the value list. You can loop through a sorted version of the .keys I guess. Maybe theres an easier way but yeah.. – Banana Jan 10 '18 at 09:17

2 Answers2

0

Please check whether this works -

import collections
dicti_1 = {'2017-09-01': ['PRUEBAPRUEBA', 'PRUEBA123123'],  
 '2017-11-08': ['PRUEBAPRUEBA', 'PRUEBA123123'], 
 '2017-10-03': ['PRUEBAPRUEBA', 'PRUEBA123123'],
 '2017-12-03': ['PRUEBA123123']}

dicti_2 = collections.OrderedDict(sorted(dicti_1.items()))
print(dicti_2)
my_dict2 = { z:x for x,y in dicti_2.items() for z in y }

print(my_dict2)
output = {}
for key, value in my_dict2.items():
    if value in output:
        output[value].append(key)
    else:
        output[value] = [key]
print(output)
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
0

My approach would be first to sort a dict according to keys and then store unique items from values of dict into a brand new dictionary.

for date in sorted(d.keys(), reverse=True):
    for l in d[date]:
        if l not in new_dict:
            new_dict[l] = date

It will produce output like below

{'PRUEBA123123': '2017-12-03', 'PRUEBAPRUEBA': '2017-11-08'}
Arunesh Singh
  • 3,489
  • 18
  • 26
  • this does not match the OP output format. If the same latest date is for multiple values, the value list of the desired output has to be appended. Check my ans @ge00rge – Vivek Kalyanarangan Jan 10 '18 at 09:54
  • @VivekKalyanarangan I assumed the questioner can do it on his/her own as it is trivial. Otherwise, he/she would have asked me. – Arunesh Singh Jan 10 '18 at 09:55