how to set dictionary if select key is repeats with python?

Question:

I’m using python 3 and I have a dictionary containing some scheduler data. Now I want to set that if a day name repeats in the data list.

here is the case and code :

data = [
    {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Saturday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None},
    {'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
]

as you can see Saturday is 2 times in the data list. the second one is full-day false and close-day true. Now I want that to stay there and the first Saturday object removes from the list.
And this thing with all days which come in the list multiple times.

Asked By: Ahmed Yasin

||

Answers:

You can use a dictionary with day as the key for this (using dictionary comprehension):

data = list({d['day']: d for d in data}.values())

This will keep only the last occurrence of each day.

The above is equivalent to:

'''
data_dict will be:
{
   'Monday': {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
   'Tuesday': {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
   ...
}
'''
data_dict = {}

for d in data:
    data_dict[d['day']] = d

data = list(data.values())

data_dict is a dictionary of day:data. So for each day, data_dict will store its latest entry from data (as older entries will be overridden).

Answered By: Yevhen Kuzmovych

for-loop will find a saturday you don’t need and pop it

data = [
{ 'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{'day': 'Saturday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None},
{'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None} 
]

for e,i in enumerate(data):
    if i['day'] == 'Saturday' and i['full_day']:
        data.pop(e)

for i in data:
    print(i)

# {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None}
# {'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
Answered By: Dmitriy Neledva
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.