0

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

  • Does this answer your question? [Find an element in a list of tuples](https://stackoverflow.com/questions/2191699/find-an-element-in-a-list-of-tuples) – Simon S. Oct 22 '22 at 18:10
  • 1
    Does this answer your question? [Does Python have a string 'contains' substring method?](https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method) – kylieCatt Oct 22 '22 at 18:15
  • Yes this answers my question also. Thank you. – Python_Slayer Oct 22 '22 at 18:22

2 Answers2

0

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)]
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
0

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)
user238795
  • 47
  • 5