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 get items of list

I have these code lines:

TheShawshankRedemption = ['drama' , 1994, 'Frank Darabont', 'Tim Robbins', 'Morgan Freeman', 'Bob Gunton', 'Stephen King']
TheGodfather = ['crime', 'drama', 'Francis Ford Coppola', 1972, 'Mario Puzo', 'Marlon Brando','Al Pacino', 'James Caan']
TheDarkKnight = ['action', 'crime', 'drama', 2008, 'Christopher Nolan', 'Jonathan Nolan', 'David S. Goyer', 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart']
SchindlersList = ['biography', 'drama', 'history', 1993, 'Steven Spielberg', 'Liam Neeson',' Ralph Fiennes',' Ben Kingsley']
TheLordoftheRingsTheReturnoftheKing= ['action', 'adventure', 'drama', 2003, 'Peter Jackson', 'J.R.R. Tolkien', 'Elijah Wood', 'Viggo Mortensen', 'Ian McKellen']
PulpFiction = ['crime', 'drama', 1994, 'Quentin Tarantino', 'John Travolta', 'Uma Thurman', 'Samuel L. Jackson']
FightClub = ['drama', 1999, 'David Fincher', 'Brad Pitt', 'Edward Norton', 'Meat Loaf']
Movies = [TheShawshankRedemption, TheGodfather, TheDarkKnight, SchindlersList, TheLordoftheRingsTheReturnoftheKing, PulpFiction, FightClub]

when i say,

print(Movies[0])

i get this result:

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

['drama' , 1994, 'Frank Darabont', 'Tim Robbins', 'Morgan Freeman', 'Bob Gunton', 'Stephen King']

this is first element of Movies array.

what i want to get as a result is TheShawshankRedemption.
how can i get this result. Thanks

>Solution :

Well as far as I can get it you want to reference the movie data to their name, in that case it will be better to go for dictionary rather than this

movies = {"TheShawshankRedemption" : ['drama' , 1994, 'Frank Darabont', 'Tim Robbins', 'Morgan Freeman', 'Bob Gunton', 'Stephen King'],
"TheGodfather" : ['crime', 'drama', 'Francis Ford Coppola', 1972, 'Mario Puzo', 'Marlon Brando','Al Pacino', 'James Caan'],
"TheDarkKnight" : ['action', 'crime', 'drama', 2008, 'Christopher Nolan', 'Jonathan Nolan', 'David S. Goyer', 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart']}

print(movies.keys()[0])

Now you can see you can simply access dictionary keys to get the desired result while making your reference to movie data as well.

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