# Carlys Clippers / Looping Index's

Hi,

I’m working on the Carly Clippers project and having trouble with the final question (below). I am new to programming find the concept of list comprehensions confusing, and thus would rather write a normal loop to solve this. You seem to need to use loops at an index position but no where in the previous lessons is this discussed. Can you explain how this could be written in the form of a loop as opposed to a list comprehension and how looping using index position works?

Carly thinks she can bring in more customers by advertising all of the haircuts she has that are under `30` dollars.
Use a list comprehension to create a list called `cuts_under_30` that has the entry `hairstyles[i]` for each `i` for which `new_prices[i]` is less than `30`.
You can use `range()` in your list comprehension to make `i` go from `0` to `len(new_prices) - 1`

new_prices = [25, 20, 35, 15, 15, 30, 45, 30]

2 Likes

It’s just an abbreviated way to create a new list. But, I get it, it can be confusing, but the basic syntax is:
an expression followed by a `for` clause, followed by 1 or more `if` clauses.

It’s similar to what you (probably) wrote in a few steps prior with,
`new_prices = [price - 5 for price in prices]`

I thought list indices were covered in the grade book project?
What code have you written so far? How would you create a new list called, `cuts_under_30` ?

Here’s some explanation of list comprehensions that might help:
https://docs.python.org/3.10/tutorial/datastructures.html?highlight=list%20comprehension#list-comprehensions

And, this also might be helpful:

1 Like

Hi,

Thanks for your response. List indicies were covered previously but they were not used in any of the loop examples / practices which why I was initially confused.

I completed the exercise as per the instructions but for my own understanding am trying to figure out how this step could be done using a for / while loop as opposed to a list comprehension. For new prices, I was able to do this with the below code:

``````new_prices = []
For price in prices:
new_prices.append (price - 5)
print(new_prices)
``````

How would I do something similar to print the haircuts under \$30?

To be clear, I am asking for the answer here!!! Thanks again.

Hello,

There are two ways to tackle this problem; you can use a list comprehension or a regular for loop. It is generally considered more “Pythonic” to use list comprehensions when the task is appropriate. In your case, if you would like to create a list of haircuts under \$30, the list comprehension would look like this.

``````haircut_prices = [30, 40, 15, 20, 10, 55]
haircuts_under_30 = [price for price in haircut_prices if price < 30]

print(haircuts_under_30)
``````

The for loop version of this same list comprehension would look like this.

``````haircut_prices = [30, 40, 15, 20, 10, 55]
haircuts_under_30 = []

for price in haircut_prices:
if price < 30:
haircuts_under_30.append(price)

print(haircuts_under_30)
``````

List comprehensions are difficult at first because everything is basically on a single line, but it is essentially the same thing. What helped me when I first started with list comprehensions is to first create the for loop version, and then try to consolidate that down to the list comprehension version.

Hope that helps!

1 Like

This is helpful, but how would you do it you wanted to print the names of the hairstyles associated with those prices at a given index position?

new_prices = [25, 20, 35, 15, 15, 30, 45, 30]
hairstyles = [“bouffant”, “pixie”, “dreadlocks”, “crew”, “bowl”, “bob”, “mohawk”, “flattop”]

Assuming the prices and haircuts are one-to-one, you can use the `zip` method.

``````new_prices = [25, 20, 35, 15, 15, 30, 45, 30]
hairstyles = ["bouffant", "pixie", "dreadlocks", "crew", "bowl", "bob", "mohawk", "flattop"]
hairstyles_under_30 = []

# https://www.w3schools.com/python/ref_func_zip.asp

# The for loop version

for price, hairstyle in zip(new_prices, hairstyles):
if price < 30:
hairstyles_under_30.append(hairstyle)

print(hairstyles_under_30)

# The list comprehension version
# p = price, h = hairstyles

hairstyles_under_30 = [h for p, h in zip(new_prices, hairstyles) if p < 30]

print(hairstyles_under_30)
``````

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.