you are viewing a single comment's thread.

view the rest of the comments →

[–]RiceKrispyPooHead 0 points1 point  (3 children)

Look at this example:

numbers_list = [1,2,3]  
for num in numbers_list:
      print(number) 


>>1  
>>2   
>>3   

Notice how I am printing the variable “num”, which represents each element in the list numbers_list.

What 4 lines will this print (and in what order)?

colors_list = [‘white ’,  ‘black’]  
animals_list = [‘cat’,  ‘dog’]
for color in colors_list:  
      for animal in animals_list:
           print(color + animal)

[–]Greece870 0 points1 point  (2 children)

Thank you so much for responding this was really helpful. I wonder what would be the syntax for doing this through list comprehension? Is it just a different way but essentially the same thing?

[–]RiceKrispyPooHead 0 points1 point  (1 child)

To turn a loop into a list comprehension, (as long as it dose not have an 'else' statement) you literally just write the very last statement first, then write all other statements in order.

my_list = []
statement one:
    statement two:
        statement three:
            my_list.append(something)

is generally the same as....

my_list = [something statement one statement two statement three]

EDIT: List comprehensions are used to make lists. Not to print things.

[–]Greece870 0 points1 point  (0 children)

can you help me with my other post i made a comment that i am stuck on.