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

I have a list and i want to print a specific string from it how can i do that?

this is how my data looks like

So far I have done this but this returns the movie name but i want the year 1995 like this in separate list.

moviename=[]
for i in names:
    moviename.append(i.split(' (',1)[0])

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 :

One issue with the code you have is that you’re getting the first element of the array returned by split, which is the movie title. You want the second argument split()[1].

That being said, this solution won’t work very well for a couple of reasons.

  1. You will still have the second parenthesis in the year "1995)"
  2. It won’t work if the title itself has parenthesis (e.g. for Shanghai Triad)

If the year is always at the end of each string, you could do something like this.

movie_years = []
for movie in names:
  movie_years.append(movie[-5:-1])
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