0

I have all my code complete and it runs properly my issue is that when I print two movies for the same year the print in the wrong order I need them to print in the order of the list. This only happens with movies printing in the same year can anyone see where in my code the problem is?

def main():

    movieCollectionDict = {"Munich":[2005, "Steven Spielberg"],

                           "The Prestige": [2006, "Christopher Nolan"],

                           "The Departed": [2006, "Martin Scorsese"],

                           "Into the Wild": [2007, "Sean Penn"],

                           "The Dark Knight": [2008, "Christopher Nolan"],

                           "Mary and Max": [2009, "Adam Elliot"],

                           "The King\'s Speech": [2010, "Tom Hooper"],

                           "The Help": [2011, "Tate Taylor"],

                           "The Artist": [2011, "Michel Hazanavicius"],

                           "Argo": [2012, "Ben Affleck"],

                           "12 Years a Slave": [2013, "Steve McQueen"],

                           "Birdman": [2014, "Alejandro G. Inarritu"],

                           "Spotlight": [2015, "Tom McCarthy"],

                           "The BFG": [2016, "Steven Spielberg"]}

    promptForYear = True

    while promptForYear:

        yearChoice = int(input("Enter a year between 2005 and 2016:\n"))

        if yearChoice<2005 or yearChoice>2016:

            print("N/A")  

        else:

            for key, value in movieCollectionDict.items():

                if value[0] == yearChoice:

                    print(key + ", " + str(value[1]) )

            promptForYear = False         

    menu = "MENU" \

    "\nSort by:" \

    "\ny - Year" \

    "\nd - Director" \

    "\nt - Movie title" \

    "\nq - Quit\n"

    promptUser = True

    first_time = True

    while promptUser:

        if first_time == True:

            print()

            first_time = False

        print('MENU\n''Sort by:\n''y - Year\n''d - Director\n''t - Movie title\n''q - Quit\n')

        userChoice = input("Choose an option:\n")

        if userChoice == "q":

            promptUser = False

        elif userChoice=="y":

            year_sorted = {}           

            for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[1], item[0])):

                year = value[0]

                title = key

                director = value[1]

                if year not in year_sorted:

                    year_sorted[year] = [[title, director]]

                else:

                    year_sorted[year].append([title, director])           

            for year in sorted(year_sorted):

                print (year,end=':\n')

                movies = year_sorted[year]

                for movie in sorted(movies, key = lambda x:x[0]):

                    print("\t"+movie[0] + ", " + movie[1])

                print()

        elif userChoice == "d":

            director_sorted = {}           

            for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[1][1])):

                year = value[0]

                title = key

                director = value[1]

                if director not in director_sorted:

                    director_sorted[director] = [[title, year]]

                else:

                    director_sorted[director].append([title, year])           

            for director in sorted(director_sorted):

                print (director,end=':\n')

                movies = director_sorted[director]

                for movie in sorted(movies, key = lambda x:x[0]):

                    print("\t"+movie[0] + ", " + str(movie[1]))           

                print()

        elif userChoice == "t":

            for key, value in sorted(movieCollectionDict.items(), key=lambda item: (item[0], item[1])):

                print(key,end=':\n')

                print("\t" + str(value[1]) + ", " + str(value[0])+"\n")

        else:

            print("Invalid input")

main()

ex. 2006 should print:

The Prestige, Christopher Nolan

The Departed, Martin Scorsese

mine prints:

The Departed, Martin Scorsese

The Prestige, Christopher Nolan

Mert Köklü
  • 2,183
  • 2
  • 16
  • 20

2 Answers2

0

As described here you can't influence the order of items in dictionary.

What you can do, though is get sorted list of keys and iterate over them. Like so:

keys = list(movieCollectionDict.keys())
keys.sort()
for key in keys:
    if movieCollectionDict[key][0] == yearChoice:
        print(key + ", " + str(movieCollectionDict[key][1]))
dmigo
  • 2,849
  • 4
  • 41
  • 62
0

Before python 3.7 dictionaries did not remember insertion order, but you could use an orderedDict. Alternatively if working in earlier python versions does not matter you can switch to python 3.7

Pete
  • 79
  • 7