Have some curious result playing with "for x in list"

Hello there,

I’ve been doing the Python intro course for a bit and a played with the project on lists to gain a better understanding of how everything is working in this.
Anyway, here’s the “issue”:
(Project link : https://www.codecademy.com/courses/learn-python-3/projects/lens-slice?action=resume_content_item)
Here is what i’ve wrote (project is completed, as i said, i’m just toying with commands here)

toppings = ["pepperoni","pineapple","cheese","sausage","olives","anchovies","mushrooms"]
prices = [2,6,1,3,2,7,2]
num_pizzas = len(toppings)
print("We sell " + str(num_pizzas) + " different kinds of pizza!")

pizzas = list(zip(prices,toppings))

for x in pizzas:
  print(x[1] + " pizza: " + str(x[0]) + "$")
  
print()
pizzas.sort()
print("Now pizzas in sorted order")

for x in pizzas:
  print(x[1] + " pizza: " + str(x[0]) + "$")
  
#return the [1] element of a list
def return_2nd_element(elem):
  return elem[1]
#use the return_2nd_element to sort with the string instead of the number
sorted_pizza_2 = sorted(pizzas,key=return_2nd_element,reverse = True)

cheapest_pizza = pizzas[1]
print()
print("Cheapest pizza of the store: " + str(cheapest_pizza[1]))
priciest_pizza = pizzas[-1]
print()
print("Priciest pizza of the store: " + str(priciest_pizza[1]))
three_cheapest = []
for x in range(3):
  three_cheapest += pizzas[x][1]
print()
print("Cheapest three pizzas of the store: " + str(three_cheapest))


num_two_dollar_slices = prices.count(2)
print()
print("There is " + str(num_two_dollar_slices) + " pizza(s) at 2$")

print(pizzas[0][1])

My issue is that the command :

three_cheapest = []
for x in range(3):
  three_cheapest += pizzas[x][1]
print()
print("Cheapest three pizzas of the store: " + str(three_cheapest))


print “Cheapest three pizzas of the store: [ ‘c’, ‘h’, ‘e’, ‘e’, ‘s’, ‘e’, ‘m’, ‘u’, ‘s’, ‘h’, ‘r’, ‘o’, ‘o’, ‘m’, ‘s’, ‘o’, ‘l’, ‘i’, ‘v’, ‘e’, ‘s’]” and i don’t get why my three pizza strings are sliced like this (huhu).

I’ve tried googling a bit but havn’t found an answer there yet, any clues ?
(Also, there is probably other/better methods to the same kind of iteration, i’m also interested in those, but i also want to know why the heck this is produced)

Thanks !

2 Likes

In the above, x is a value, not an index. In this case, it is a tuple.

In the above, x is an integer, as in an index.

6 Likes

Yeah, but, let say for the first iteration x == 0
So it should print pizzas[0][1] (cheese) isn’t it ? Why does it print [ ‘c’, ‘h’, ‘e’, ‘e’, ‘s’, ‘e’] ?
Wouldn’t it need a third (like something similar to pizzas[0][1][:5]) for it to print this kind of result ?

for x in pizzas:
  print(x[1] + " pizza: $" + str(x[0]))

That should work as expected.

5 Likes

Like if i do this :

for x in range(3):
  three_cheapest += pizzas[x][1]
  print(pizzas[x][1])

the print(pizzas[x][1]) print the expected :
cheese
mushrooms
olives

But in the list it’s all splited for some reasons

4 Likes

Yeah, i kinda did this with the firsts two “classy” listing of pizzas, but in this case i only needed the firsts 3 index of the pizzas list, that’s why i ended up doing it like this ^^

Try using append instead of +=.

9 Likes

Nice ! You gave me a second way of solving the issue ! While you were answering i was fidgeting with the codes to try a few things and remembered that the things like : list += 'random' gave the same kind of weird results.
It’s apprently because if it’s not wrapped into [ ] it’ll be treated as a list of character instead of a full string.
So I just did this :

for x in range(3):
  three_cheapest += [pizzas[x][1]]
  print(pizzas[x][1])


and it worked aswell as the .append you just gave me :slight_smile:

Once again @mtf , thanks for the help !

9 Likes

Addition is something you’d do between two similar things.
Two numbers.
Two colours.
Two lists.
Two strings.
Two iterables.
list + list element … nope. doesn’t make sense. different shapes.

6 Likes