Python: writing function to check list and return values or None

Viewed 122

I am really struggling to get to grips with python and figuring out how to answer the following question:

1) Accept the movies list and a category name. If the category is not in the data, prints a message that says it does not exist and returns None.

This is list it's looking at:

movies = [
{
"name": "Usual Suspects", 
"imdb": 7.0,
"category": "Thriller"
},
{
"name": "Hitman",
"imdb": 6.3,
"category": "Action"
},
{
"name": "Dark Knight",
"imdb": 9.0,
"category": "Adventure"
},
{
"name": "The Help",
"imdb": 8.0,
"category": "Drama"
},
{
"name": "The Choice",
"imdb": 6.2,
"category": "Romance"
},
{
"name": "Colonia",
"imdb": 7.4,
"category": "Romance"
},
{
"name": "Love",
"imdb": 6.0,
"category": "Romance"
}
]

And this is the code I have so far but it only returns the matches and prints for each non-match which is not what I want:

def incat(cat):  
    for m in movies:  
        if m['category'] == cat:  
            print(m['name'])  
        else:  
            print(cat + ' does not exist')  

incat('Romance')

I'm probably really missing something very easy but am just finding it super tough to wrap my head around. Any help is really appreciated.

6 Answers

You can use any() to check if any of the category matches with desired category and print that it does not if not (I'm on the assumption that you only want to print in the case when the category is not found):

def incat(cat):
    if not any(m['category'] == cat for m in movies):
        print(cat + ' does not exist') 

you can use flag for this

def incat(cat):  
    flag = false
    for m in movies:  
        if m['category'] == cat:  
            print(m['name'])  
            flag = true
    if not flag:
        print(cat + ' does not exist') 
incat('Romance')

You are probably looking for a function like this:

def incat(cat):  
    f = 0
    for m in movies:  
        if m['category'] == cat:  
            f = 1
            print(m['name'])  
    if f == 0:
        print(cat + ' does not exist')
        return None

This function first sets a flag to 0 and then matches each movie category with the one provided using a for loop. If a movie category matches the category provided, it sets the flag to 1 and prints out the movie name. After checking all the movies, it checks the flag. If it is still 0, it means that no movie category matched the category provided. So, it prints the appropriate message and returns None.

You could also refactor your code a bit. I would create a set of categories(unique categories) and then I would check if incoming category exists. This is my take:

movies = [
{
"name": "Usual Suspects", 
"imdb": 7.0,
"category": "Thriller"
},
{
"name": "Hitman",
"imdb": 6.3,
"category": "Action"
},
{
"name": "Dark Knight",
"imdb": 9.0,
"category": "Adventure"
},
{
"name": "The Help",
"imdb": 8.0,
"category": "Drama"
},
{
"name": "The Choice",
"imdb": 6.2,
"category": "Romance"
},
{
"name": "Colonia",
"imdb": 7.4,
"category": "Romance"
},
{
"name": "Love",
"imdb": 6.0,
"category": "Romance"
}
]

def check_category(category, movies):
    # create a set of categories
    categories = {cat['category'] for _,cat in enumerate(movies)}

    if category not in categories:
        print(f'Category: {category} does not exist')
        return None
    else:
        return True

check_category('t',movies)

Add a last one, with set and map.

    movies = [ { "name": "Usual Suspects", "imdb": 7.0, "category": "Thriller" }, { "name": "Hitman", "imdb": 6.3, "category": "Action" }, { "name": "Dark Knight", "imdb": 9.0, "category": "Adventure" }, { "name": "The Help", "imdb": 8.0, "category": "Drama" }, { "name": "The Choice", "imdb": 6.2, "category": "Romance" }, { "name": "Colonia", "imdb": 7.4, "category": "Romance" }, { "name": "Love", "imdb": 6.0, "category": "Romance" } ]

def incat(movie_list, cat):
    my_cat = set(map(lambda movie : movie.get("category"), movie_list))
    if cat not in my_cat:
        print("not found")
    retuen None

You can use a list comprehension to list all of the information related to your category:

def in_cat(cat):
    new = []
    result = [i for i in movies if i['category'] == cat]
    new.append(result)
    print(new)


in_cat('Thriller')

Output:

[[{'name': 'Usual Suspects', 'imdb': 7.0, 'category': 'Thriller'}]]
Related