def get_author(data, search_author): # using here argument unpacking for movie, (year, author) in data.items(): if search_author == author: print(movie, year, author) get_author(movies, 'Steven Spielberg')Without argument unpacking:
def get_author(data, search_author): # using here tuple unpacking for movie, value in data.items(): year = value[0] author = value[1] if search_author == author: print(movie, year, author) get_author(movies, 'Steven Spielberg')If you want to sort them by X, use the
sorted
builtin function together with the key argument.def by_author(item): ''' Returns the author of an item ''' return item[1][1] def by_year(item): ''' Returns the year of an item ''' return item[1][0] def by_movie(item): ''' Returns the movie of an item ''' return item[0] def get_author_sorted(data, search_author, sort_by): for movie, (year, author) in sorted(data.items(), key=sort_by): if search_author == author: print(movie, year, author) get_author_sorted(movies, 'Steven Spielberg', by_year)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!