100 days of code: Day 20 - list to dictionary

Last modified on 2020-06-28

Source: 100-days-of-code.

A little program that starts with a list of tags and loops through the items to create a key-value pair of date key and temperature value …

tag_list = [('2015-07-14T07:45', ':temperature: 18.5'), ('2015-07-13T06:45', ':temperature: 17.0')]
date_temp = {}

print(f"Start list: ", tag_list)

for tag in tag_list:
    #Change 'YYYY-MM-DDTHH:MM' to 'YYYY-MM-DD'
    date_key = tag[0][0:10]
    # Change ':temperature: NN.N' to 'NN.N'
    temp_value = tag[1][-4:]
    date_temp[date_key] = temp_value

print("End dictionary: " + f"{date_temp}")

Run …

$ ./tidytag.py 
Start list:  [('2015-07-14T07:45', ':temperature: 18.5'), ('2015-07-13T06:45', ':temperature: 17.0')]
End dictionary: {'2015-07-14': '18.5', '2015-07-13': '17.0'}

Happy hacking!