Looping over a list creating a new list

i am at the course “learn python 3”. in the exercise about carlys clippers (https://www.codecademy.com/courses/learn-python-3/projects/carlys-clippers)
i did write the following code:
hairstyles = [“bouffant”, “pixie”, “dreadlocks”, “crew”, “bowl”, “bob”, “mohawk”, “flattop”]
prices = [30, 25, 40, 20, 20, 35, 50, 35]
new_lst =
for i in prices:
if i < 30:
new_lst.append(hairstyles[prices.index(i)])
print(new_lst)

why does it return: [‘pixie’, ‘crew’, ‘crew’]
and not: [‘pixie’, ‘crew’, ‘bowl’]

How do you reach bowl as the third item? And, when you do that, is that what you want it to be doing, or what you reach when doing what it says in your code? (Make sure you’re not ignoring the code when reading it, and instead “reading” what you think it does)

You should probably not be searching for values in your list, isn’t it positions that you’re interested in? So use the positions, not the values.

You should also generally not split up your data into multiple lists. Make a group out of each data point instead, and then make a list of data points.

in the exercise those two lists are given. so your advice would be to transform them into a dictionary?

That wouldn’t really help you if each group has more than two values, and you don’t need the lookup-by-key behaviour that dict has so it’s not really doing anything for you.

So it’s not a great match but, sure, it can represent pairs.

What you’re looking for is to create a group for each entry, not a thing to contain it all.

When you have groups you can put those groups in a list

namedtuple from collections module would be suitable. regular tuples would do the job too and are a bit more beginner friendly (but not quite as nice)
you’re already using a grouping datatype as well. list. having multiple types mixed in a list is a little bit weird, but not so weird in this context
creating your own datatype with attributes for those particular fields would work too, and is essentially what namedtuple does for you

you mean something along those lines:

list = [["A",0], ["B",1], ["C",0], ["D",2], ["E",2]]

?

right.

ill give it a try
thanks for your help

The main argument against using lists (or tuples) is that you would now be referring to fields by their indices and that’s pretty awkward

It would be better to refer to them by field (name, price)

That’s what namedtuple provides:

>>> from collections import namedtuple
>>> Hairstyle = namedtuple('Hairstyle', ['name', 'price'])
>>> some_hair = Hairstyle('bouffant', 30)
>>> some_hair
Hairstyle(name='bouffant', price=30)
>>> some_hair.name
'bouffant'
>>> some_hair.price
30

thank you so much i am busy with some other code that i want to finish atm but ill have a look at later today or tomorrow