movies = ["The Holy Grail", 1975, "Terry Jones & Terry Gilliam", 91,
["Graham Chapman",
["Michael Palin", "John Cleese", "Terry Gilliam", "Eric Idle", "Terry Jones"]]]


In the above code, how does [4][1][3] result in "Eric Idle"? It looks to me like the 4 selects Eric Idle becaue it's the 4th sting in that list, it also looks to me like the 3 selects that list because Eric Idle is in the 3rd list and I have no diea what the 1 might be going. And maybe I'M wrong all together.

5 Years
Discussion Span
Last Post by ~s.o.s~

as I see it it's like this

the checking starts with 0 (list indeces start with 0) so it would look like

movies [ 0 , 1 , 2 , 3 , [ 0 , [ 0 , 1 , 2 , 3 ] ] ]

where the 4th one is a list and inside it the second one is a list

so from left to right [4][1][3] should point to Eric Idle

Edited by zeroliken


Bump tot he above poster, it is most certainly better to store variables so they may be accessed by attribute (eg movie.director)


Look at this:

# named tuples have named indexes they behave similar to class
# instances but require no more memory than regular tuples
# the record_list of named tuples can be saved(dumped) and loaded
# with Python module pickle

import collections as co

# create the named tuple
Movies = co.namedtuple('Movies',
         'title, year, director, minutes, cast')

record_list = []
# create one record for the record_list
title = "The Holy Grail"
year = 1975
director = "Terry Jones & Terry Gilliam"
minutes = 91
cast = [
'Graham Chapman',
'John Cleese',
'Terry Gilliam',
'Eric Idle',
'Terry Jones',
'Michael Palin']
record_list.append(Movies(title, year, director, minutes, cast))

# now you can do things like this
actor = 'John Cleese'
for movies in record_list:
    if actor in movies.cast:
        print("%s acted in %s" % (actor, movies.title))

'''my result -->
John Cleese acted in The Holy Grail

Edited by Ene Uran: pickel

Votes + Comments
the way to go

the checking starts with 0 (list indeces start with 0)

Lists don't have indexes, lists use offsets. So "some_list[5]" means offset 5 from the beginning, or skip over the first 5 elements=beginning of the sixth. It comes from the beginning days of computers when programmers had to do everything for themselves. If a list contained fixed length elements and you wanted to start reading from the beginning of the sixth element, you would move the memory or file pointer ahead 5*length-of-element. For example, if the list contained 2 byte integers you would skip over the first 10 bytes and read the next 2. If you want the first element, you would skip 0 bytes, etc. Today, Python takes care of this for you.

Edited by woooee


class Movie():

This defines an old style class. A better way would be to have:

class Movie(object):

This is suggested because old style classes don't play well with type and other new Python constructs. E.g.

>>> class A(): pass
>>> type(A())
<type 'instance'>  <--- not good enough
>>> class A(object): pass
>>> type(A())
<class '__main__.A'> <---- now we are talking

Lists don't have indexes

Actually, they do. From the official tutorial:

Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:

Edited by ~s.o.s~

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.