Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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:

>>> sequels_that_end_with_2 = [(film, rating) for (film, rating) in films if film.endswith('2')]
>>> sequels_that_end_with_2
[('Kill Bill: Vol. 2', 8.0)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading