Print the number of movies that was watched by more than 3 people I want to create a program that can print the number of movies that was watched by more than 3 people (data stored in a nested list as below ).

list_movies = [('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald']),
               ('Terminator',['Anne','Johnny','Peter','Ronald','Neville'])]



count2 = 0
for (movie,name) in list_movies:
    count = 0
    for (name) in list_movies:
        if name != '':
            count += 1
    if count &gt; 3:
        count2 += 1

print(count)
print(count2)
Make it a function

This should really be a function for test ability, reuse ability

It would need to be a function which accepts 2 parameters (The list we want to query, the amount of people have had to watch it)

And it will return the amount of times this happened


Add doc strings to said function, so after a while when you come back to this code, you still understand what you have written


For example:

def list_movies(movie_list, n):
    """
    a method what returns the count of movies watched by more then n people

    args:
        movie_list (list): The list of movies in [(movie, [names]), ..]
        n (int): The amount of times the movies has need to be watched

    returns:
       The count of movies that has been watched by more then n people
    """

Make proper tests

if you had properly tested this code, @Mathias's comment would probably not be necessary

There are a few good modules to test functionality doctest or unittests

Let's use doctest and add some tests to the docstring:

&gt;&gt;&gt; count_movies_watched([('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald'])], 3)
1

&gt;&gt;&gt; count_movies_watched([('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald'])], 4)
0



Without even writing a single line of code, it's already more clear, what this function will do and what a correct answer will be.

Improving Code


You can use list comprehension and the built in sum to get a count of movies
Use _ for variables you don;t use later on


Netting me:

sum([1 if len(names) &gt; n else 0 for _, names in movie_list])


But this can also be improved, as @Mathias correctly states


sum will accept any iterable there is no need for the list comprehension
sum can add bools as well so no need for the awkward ternary


Netting me:

sum(len(names) &gt; n for _, names in movie_list)


Putting it all together

import doctest

def count_movies_watched(movie_list, n):
    """
    a method what returns the count of movies watched by more then n people

    args:
        movie_list (list): The list of movies in [(movie, [names]), ..]
        n (int): The amount of times the movies has need to be watched

    returns:
        The count of movies that has been watched by more then n people

    &gt;&gt;&gt; count_movies_watched([('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald'])], 3)
    1

    &gt;&gt;&gt; count_movies_watched([('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald'])], 4)
    0
    """
    return sum(len(names) &gt; n for _, names in movie_list)

if __name__ == '__main__':
    doctest.testmod()