0

I want to be able to put in the start and end year and have the names in the dictionary that are in between those years print out.

Dictionary:

dict = {'Charell Adagala': 2018, 'Sukhneet kaur': 2018, 'Mayanka Jha': 2019, 'Paul Cheakalos': 2018, 'Elizabeth(Liz)Boniface': 2013, 'Kati Illieva': 2021, 'Suzy wilson': 2018, 'Ronit Gopalani': 2019}

Start year: 2019 End year: 2021

Given those years how to print out {'Mayanka Jha', Kati Illieva', 'Ronit Gopalani'}

1 Answers1

1

Here a working implementation. As a note I change the variable name form dict to my_dict, dict is a python keyword and therefore it is not recommended using it as a variable name since it may lead to unexpected behaviour.

my_dict = {
    "Charell Adagala": 2018,
    "Sukhneet kaur": 2018,
    "Mayanka Jha": 2019,
    "Paul Cheakalos": 2018,
    "Elizabeth(Liz)Boniface": 2013,
    "Kati Illieva": 2021,
    "Suzy wilson": 2018,
    "Ronit Gopalani": 2019,
}

start_year = 2019
end_year = 2021

output = [k for k, v in my_dict.items() if v >= start_year and v <= end_year]
Matteo Zanoni
  • 3,429
  • 9
  • 27
  • 2
    Please do not use `dict` as a variable name in Python. You stomp on the built-in function by that same name. Idiomatically, you can do `2019 <= v <= 2021` for greater clarity. – dawg May 11 '21 at 20:07
  • The poster of the question called it dict, so I think he was just trying to maintain consistency. – kinshukdua May 11 '21 at 20:08
  • 1
    @kinshukdua: Good opportunity to both give an answer and some extra as well... – dawg May 11 '21 at 20:09
  • I agree, dict is not a good choice for a variable name. I was trying to maintain the same code from the question... Should I still change the variable name in the answer? (I'm still quite new to StackOverflow) – Matteo Zanoni May 11 '21 at 20:11
  • @MatteoZanoni: I replace an example from a post with `your_dict` and include the exact text I had in my first comment. You are off to a great start! – dawg May 11 '21 at 20:13
  • @dawg I edited the answer changing the variable name and including an explanation. Thanks for the advice – Matteo Zanoni May 11 '21 at 20:17