Embedded Lists

Updated on 28 Dec 2022

Lists - embedded lists

In an earlier discussion on lists we saw this

students = ['brent', 'paul', 'andrea']
grades   = ['High Distinction', 'Pass', 'Credit']

However it is also possible to nest a list inside a list.

records = [
    ['brent', 'High Distinction'],
    ['paul', 'Pass'],
    ['andrea', 'Credit']
]

This approach seems to make sense because now I can access records[1] and get a list that contains the name paul and grade Pass. I.e.

records = [
    ['brent', 'High Distinction'],
    ['paul', 'Pass'],
    ['andrea', 'Credit']
]

print(records[1])

Tip: Remember, lists have a zero based index. Hence, 1 is referring to the second item in the list, [‘paul’, ‘Pass’].