Unable to perform if conditional in list comprehension using a list in the if conditional section

I am unable to perform a if conditional where a list is < 30. It returns an error everytime. I know this isn’t right but I’m not sure how to assign this to a variable within a list comprehension. Here is the question:

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 .

Here is my code:

cuts_under_30 = hairstyles[i] for i in range(len(new_prices - 1)) if new_prices < 30]

What do you hope to accomplish by this condition: list < 30?
You answer is what changes what you do with list in regards to that condition.

#Case 1
if len(list) < 30 
#checks if number of elements in list is less than 30

#Case 2
if reduce(lambda a, b: a+b, list) < 30
#checks if sum of elements in list is less than 30

These are just some things you can do with that.
Also, your cuts_under_30 list comprehension isn’t closed. Add a [ at the start.

Can you post a link to the exercise?

2 Likes

DEBUG DEBUG look carefully :wink:

1 Like

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-python-loops/projects/carlys-clippers

Here is the link. I guess I’m trying to see if each number within the new_prices list is < 30.

@h1lo

I’m not sure what lambda does.

Found the missing [ but there is still an issue with the list and I believe it lies within the if conditional.

Don’t worry about lambda for now, glad you fixed your missing [ .
I find that whenever I have an error, I go back and reconstruct my code.
Ok so it seems like you are trying to get a hairstyle if it’s price is less than 30.
Think about the steps you would take to solve this.

  1. Maybe you could zip together the lists (hairstyles and new_prices)? Could lead to possible indexing… WINK WINK

@h1lo

Does any type of manipulation take place before the for statement in list comprehension? Trying to see where would be the best part to zip.

Well, from what I can see, your list comprehension is handling 2 different lists right now. I guess you could do something like:

cuts_under_30 = [hair[0] for hair in zip(hairstyles, new_prices) if hair[1] < 30]

Something a bit closer to your original code:

cuts_under_30 = [hairstyles[i] for i in range(len(new_prices))-1 if new_prices[i] < 30]
3 Likes

I think that your statement is trying to loop through your range(len()), but it can’t find what it’s looking for

range(len(new_prices - 1))

is trying to iterate through the range of items you have in a variable named “new_prices - 1”

2 Likes

Gosh I was so close. So are you saying that I needed to have the - 1 outside of the parenthesis?

I’m kinda new to this myself, but maybe… I think it still needs to be within the range(len()) somewhere though…

2 Likes

Err sorry. You want to subtract from len(new_prices) right? Then:

range(len(new_prices)-1)
2 Likes

If I didn’t have the - 1, how would that affect the loop? I tried it both ways and it returned the same results.

I really only added that because the question pretty much stated to do it.

lst = [1, 2, 4, 6, 7, 8, 9, 10]
len(lst)#is 8
for i in range(len(lst)-1):
  print(i)
0
1
2
3
4
5
6

for i in range(len(lst)):
  print(i)
0
1
2
3
4
5
6
7

Hmm… wouldn’t that throw an IndexError though?

The first loop wouldn’t print the full size of the list however the second one would. The first loop just leaves off the last value.

Yeah, so I’m wondering then why you got the same result. You wouldn’t be able to reach the last element of new_prices right?

That’s correct. Fortunately the last item in new_prices is 30.

LOL! Ok, then. In the future, I suggest not just blindly going along with what the answer is. If you take steps to solve a problem you can find an alternative solution. Try changing the last element in new_prices to 25, and run your program again both ways, with the -1 and without and see if you get the same answer this time.