Click here to Skip to main content
15,174,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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'])]

What I have tried:

Python
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 > 3:
        count2 += 1
    
print(count)
print(count2)
Posted
Updated 21-Oct-18 22:25pm

1 solution

You inner loop should iterate over the second item of the tuple:
Python
list_movies = [('Spiderman 3', ['John', 'jake','Ronald']),('Gravity',['james','jake','john','gerald']),
               ('Terminator',['Anne','Johnny','Peter','Ronald','Neville'])]



count2 = 0
for (movie,viewers) in list_movies:
    count = 0
    for (name) in viewers:
        if name != '':
            count += 1
    if count > 3:
        count2 += 1

print("last movie viewers: %d" % count)
print("count of movies having had more than three viewers: %d" % count2)
   
Comments
[no name] 22-Oct-18 3:42am
   
Is it a tuple or a nested list?
CPallini 22-Oct-18 4:02am
   
There is a list (the outer list) of tuples. Each tuple contains in turn a string (first item) and a list (the inner list) of strings (second item).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900