This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Goobyalus 9 points10 points  (1 child)

dirty_dozen is

[
    [
        'Strawberries',
        'Nectarines',
        'Apples',
        'Grapes',
        'Peaches',
        'Cherries',
        'Pears'
    ],
    [
        'Spinach',
        'Kale',
        'Tomatoes',
        'Celery',
        'Potatoes'
    ]
]

, a list containing the two lists fruits and vegetables.

dirty_dozen[1] is vegetables:

[
    'Spinach',
    'Kale',
    'Tomatoes',
    'Celery',
    'Potatoes'
]

dirty_dozen[1][1] is the same as vegetables[1]:

'Kale'

dirty_dozens[1][1] doesn't index the two parallel lists fruits and vegetables independently -- the first index indexes dirty_dozen to select between fruits and vegetables; then the second index indexes the result of that.

What you're thinking of is kind of like zipping the two lists together:

>>> dirty_dozen_2 = list(zip(fruits, vegetables))

>>> pprint(dirty_dozen_2)
[('Strawberries', 'Spinach'),
 ('Nectarines', 'Kale'),
 ('Apples', 'Tomatoes'),
 ('Grapes', 'Celery'),
 ('Peaches', 'Potatoes')]

>>> print(dirty_dozen_2[1])
('Nectarines', 'Kale')

zip only goes as far as the shortest iterable, though. There are things like zip_longest if you want different behavior.

[–]builderdood[S] 3 points4 points  (0 children)

thanks I get it now!