'Trying to print something if it is NOT in a list

movies = [[1939, 'Gone With the Wind', 'drama'],
      [1943, 'Casablanca', 'drama'],
      [1961, 'West Side Story', 'musical'],       
      [1965, 'The Sound of Music', 'musical'],
      [1969, 'Midnight Cowboy', 'drama'],
      [1972, 'The Godfather', 'drama'],
      [1973, 'The Sting', 'comedy'],   
      [1977, 'Annie Hall', 'comedy'],
      [1981, 'Chariots of Fire', 'drama'],
      [1982, 'Gandhi', 'historical'],            
      [1984, 'Amadeus', 'historical'],
      [1986, 'Platoon', 'action'],
      [1988, 'Rain Man', 'drama'],
      [1990, 'Dances with Wolves', 'western'],
      [1991, 'The Silence of the Lambs', 'drama'],  
      [1992, 'Unforgiven', 'western'],
      [1993, 'Schindler s List', 'historical'], 
      [1994, 'Forrest Gump', 'comedy'],
      [1995, 'Braveheart', 'historical'],
      [1997, 'Titanic', 'historical'],
      [1998, 'Shakespeare in Love', 'comedy'],
      [2001, 'A Beautiful Mind', 'historical'],
      [2002, 'Chicago', 'musical'],
      [2009, 'The Hurt Locker', 'action'],
      [2010, 'The Kings Speech', 'historical'],
      [2011, 'The Artist', 'comedy'],
      [2012, 'Argo', 'historical'],
      [2013, '12 Years a Slave', 'drama'],
      [2014, 'Birdman', 'comedy'],
      [2016, 'Moonlight', 'drama'],
      [2017, 'The Shape of Water', 'fantasy'],
      [2018, 'Green Book', 'drama'],               
      [2019, 'Parasite', 'drama'],
      [2020, 'Nomadland', 'drama'] ]



category = input("Enter a category: ")
    for x in movies:
        if category in x[2]:
            print("\n",x[1])
    if category not in x: 
        print("No matches")

This is a small snippet of my program but I want to print "No matches" if the category is not in the list, but it prints it even if the category is in the list. It works only if the category entered is NOT in the list. I've worked on this for so long and I can't find an answer anywhere.



Solution 1:[1]

There are some fixes and optimizations that may apply:

  1. I think that you want to extract all the titles from the list with matching category, i.e. if I input drama, it will print all the movies with drama. So you just need to compare, no need to recheck with in, as it's more higher cost for unnecessary checking with the year and the movie title. In your data shown in your question, every movies have only one category for each, so this optimization can be applied.

  2. You just need a flag that indicates if the category actually exists or not.

category = input("Enter a category: ")
exist = False  # Define the flag
for x in movies:
    if category == x[2]:  # Check if the movie's category is same as the user input
        print("\n",x[1])
        exist = True      # Change if found any
        
if not exist:
    print("No Matches!")

Solution 2:[2]

It seems like the easiest way to handle this is to create a list of all the matching films titles. If that list is empty, print not found:

query = input("Enter a category: ")
found = [title for date, title, category in movies if category == query]

if found:
    print('\n'.join(found))
else:
    print("Not found")

This will works like:

> Enter a category: musical
West Side Story
The Sound of Music
Chicago


> Enter a category: scifi
Not found

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhana D.
Solution 2 Mark