Lewis
Lewis

Reputation: 1575

How to append a nested list in Python?

How can I divide all of the data in the second row by two?

recipe  = [
    ['eggs', 'flour', 'meat'],
    [4, 250, 5],
    ['large','grams', 'kg'],
]

I've tried starting with

for row[2] in recipe:

But I get an error saying:

Traceback (most recent call last):

File "/Users/g/Documents/reicpe.py", line 7, in for row[2] in meat_pie:

NameError: name 'row' is not defined

Upvotes: 0

Views: 2224

Answers (4)

AlvaroAV
AlvaroAV

Reputation: 10563

You can also use list comprehension and do it in one single line without the for-loop:

recipe[1] = [num / 2 for num in recipe[1]] 

Code Explanation [num / 2 for num in recipe[1]]:

  • recipe[1]: This is a list, it's the second element of recipe list

    The second element of recipe is: [4, 250, 5]

  • for num in recipe[1]: This mean that we're going to loop over the elements of recipe[1], so num it's a variable, and it's value changes over the list elements in each iteration.

  • num / 2: It's obvious that we get num and divide by 2

Upvotes: 3

Tony Suffolk 66
Tony Suffolk 66

Reputation: 9724

recipe  = [
    ['eggs', 'flour', 'meat'],
    [4, 250, 5],
    ['large','grams', 'kg'],
]

Leaving aside this would be far better as a dictionary, if you want to divide the quantities by two, and change what you have stored :

for quantity, index in enumerate(recipe[1])
    recipe[1][index] = quantity/2

A better way would be to use a dictionary, which allows you to give names to data items :

recipe = {"eggs":{"quantity":4, "measurement":"large"},
         "flour":{"quantity":250,"measurement":"grams"}, 
         "meat":{"quantity":5,"measurement":"kg"}}

and now division by two becomes :

for ingredient in recipe:
    recipe[ingredient]["quantity"] = recipe[ingredient]["quantity"]/2

and print the recipe becomes :

for ingredient in recipe:
    print "{} {} {}".format(recipe[ingredient]["quantity"], recipe[ingredient]["measurement"], ingredient)

this generates :

4 large eggs
250 grams flour
5 kg meat

and no faffing about with index numbers etc.

Upvotes: 1

nerdwaller
nerdwaller

Reputation: 1863

You're trying to iterate on the wrong portion. When you use the statement: for i in list, i is created as a new variable (as if you had just stated i = 5). So at that point, it does not have a getter (index or otherwise) for element 5 (and it is not a valid variable name).

To fix your iteration issue try:

for row in mylist[index]:

That will iterate over the list at index in mylist, of course remember that lists are 0 indexed (your numbers are at index 1).

You're going to have another issue shortly with updating the values in your array, however, since the process will create a copy the way you're doing it. A simple fix is to use enumerate (but I will leave that up to you to try out before giving it to you!)

Upvotes: 0

kyflare
kyflare

Reputation: 874

recipe[1] gives you the second list inside the recipe list. Remember, list index always start with 0.

Then:

for row in recipe[1]:

Will iterate with row taking the value of each of the values inside recipe[1].

Upvotes: 0

Related Questions