How to find a movie name in a list, which there was a part of 2 of the movie?

Question:

There is an array of movies, with tuples in it:

films = [(film_name,film_rating),...]

How do I find a film name which has the same name, but the number 2 added to it (like a part 2)?

Asked By: Python_Slayer

||

Answers:

Consider utilizing str.startswith:

>>> films = [('Kill Bill: Vol. 1', 8.2), ('Pulp Fiction', 8.9), ('Kill Bill: Vol. 2', 8.0)]
>>> kill_bill_films = [(film, rating) for (film, rating) in films if film.startswith('Kill Bill')]
>>> kill_bill_films
[('Kill Bill: Vol. 1', 8.2), ('Kill Bill: Vol. 2', 8.0)]

Or str.endswith:

>>> films_that_end_with_2 = [(film, rating) for (film, rating) in films if film.endswith('2')]
>>> films_that_end_with_2
[('Kill Bill: Vol. 2', 8.0)]
Answered By: Sash Sinha

You give very little information but if you want a movie that contains "part 2" in it’s name, then the below code should be fine:

films = [("start wars 1",9.0 ), ("star wars part 2",10.0 )]
filmSet = []
for tple in films:
    if "part 2" in tple[0]:
        filmSet.append(tple)
print(filmSet)
Answered By: user238795
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.