njBernstein
njBernstein

Reputation: 115

Accessing position of an object in a python list (3.5)

Really basic question but i can't seem to find an answer. How can I get the position of an object from a list?

for amount, ingredient in self.ingredients:
        for key in ingredient.nutrients:
            print(amount*(ingredient.nutrients[key]))
            if self.ingredients.index(ingredient) == 0:

                    amounts[key] = amount*(ingredient.nutrients[key]) 
            else:
                    amounts[key] = amount*(ingredient.nutrients[key])  + amounts[key]

Object in question (self.ingredients)

    [(300, Ingredient(Egg, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258})), (0.25, Recipe(Bread, [(820, Ingredient(Flour, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258})), (30, Ingredient(Oil, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258})), (36, Ingredient(Sugar, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258})), (7, Ingredient(Yeast, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258})), (560, Ingredient(Water, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258}))]))]

Error:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-54-85a3df33671e> in <module>()
         56 print(basic_french_toast.ingredients)
         57 
    ---> 58 print(basic_french_toast.nutrients)

    <ipython-input-54-85a3df33671e> in nutrients(self)
         40             for key in ingredient.nutrients:
         41                 print(amount*(ingredient.nutrients[key]))
    ---> 42                 if self.ingredients.index(ingredient) == 0:
         43 
         44                         amounts[key] = amount*(ingredient.nutrients[key])

    ValueError: Ingredient(Egg, {'fat': 0.0994, 'cholesterol': 0.00423, 'carbs': 0.0077, 'protein': 0.1258}) is not in list

If anyone could explain to me why this doesn't work that would be great. I don't want an explicit answer just a nudge in the right direction.

Best

Upvotes: 0

Views: 1078

Answers (2)

chepner
chepner

Reputation: 531165

list.index, as you've observed, doesn't return 0 if the object isn't found; it raises a ValueError exception. (Think about it: the function returns an index, and 0 is a valid list index.)

You just need to catch the exception:

try:
    self.ingredients.index(ingredient)
except ValueError:
    amounts[key] = 0 

amounts[key] += amount * (ingredient.nutrients[key])

Upvotes: 1

Russley Shaw
Russley Shaw

Reputation: 441

If you are talking about iterating over a list and wanting the index, you can pass the list to enumerate() and it will return its index followed by the item.

for index, item in enumerate(items):
    ...

Or if you just want to find the index of an item in your list you can use listToSearch.index(itemToFind)

Upvotes: 2

Related Questions