-1

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:

['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

Yiğit Yılmaz
  • 77
  • 1
  • 1
  • 7
  • Then change `Movies` as `["TheShawshankRedemption", ....`] – Abdul Niyas P M Jan 18 '23 at 10:40
  • TheShawshankRedemption , TheGodfather ... all of these are another arrays in Movies array. if i do what you say, how could i give reference to these arrays separetly – Yiğit Yılmaz Jan 18 '23 at 10:43
  • 3
    Use a `dict`, believe me you DON'T want to use the name of the variable. – Gameplay Jan 18 '23 at 10:45
  • It's not possible to get `TheShawshankRedemption`, because a list stores **things**, and that is a **name**. It's like if I called you on the phone and you said "hello, yes, this is Yigit", and I was upset because there was a person speaking to me, instead of some letters. – Karl Knechtel Jan 18 '23 at 10:51

1 Answers1

2

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.