ok so for loops and lists im having a hard time understanding how, where and why things are set up the way they are?

ok so how does this sum list operate?
once inside the how is everything represented? like: “item1+item2”. since thats the first thing in the list does that mean that python recognizes the first item in the list as a placeholder for something inside the “A” & “B” list? and then:
after the “for” operator you’ve got “(item1, item2)” does this just represent both list “A” & “B” being placed together in a global var?
and lastly for the zip is that referring to the prior (item1, item2) ? im just not sure how all of the separate placement works and how its represented and understood by the code?

The zip object is comprised of value pairs in tuples. Each value pair is added as the object is iterated.

Because Python is able to unpack a sequence, such as a tuple, so long as we supply the appropriate number of variables, each variable will be one value from the tuple, in the order in which they are stored.

a, b = (6, 7)
print (a * b)    #  42
1 Like

Hello, @alexmatthews62377699.

I’ll attempt to shed some light here. A list comprehension is more or less a shorthand way to populate a list with elements from another iterable. Operations can be performed on these elements prior to adding them to the list as is the case here. item1 and item2 are added together prior to being included in the list. So, where do item1 and item2 come from? They are unpacked from the tuples stored in the zip object that was created by zip(a, b). The zip function in this case is zipping a and b together. The resulting zip object is a unique iterable. Once iterated, it still exists in memory, but it is empty. It empties as it iterates. You also can’t simply print a zip object like you can other iterables such as a list. You can cast a zip object to a list, and then print it, but the zip object will then be empty. This may all still seem confusing, so let’s look at some examples:

# 2 simple lists: a = [1, 2, 3] b = [6, 5, 4] # create a zip object from the lists c = zip(a, b) # if we can visualize the zip object like so: [(1, 6), (2, 5), (3, 4)] #try printing the zip object: print(c) # click 'Run' to see # un-comment the next line to cast c to a list, and then click 'Run' again # print(list(c)) # you can see that c has been emptied by trying to cast it to a list again and print # un-comment the next line, and click 'Run' again # print(list(c)) #prints an empty list because the zip object is empty # when we unpack the zip object we use 2 variables since each element is a tuple with 2 elements # rather than a list comprehension I'll use a loop, so we can add print statements to view the # list as it is being built # un-comment the code below, and click 'Run' when ready # new_list = [] # for x, y in zip(a, b): # created a new zip object since c has been emptied already # print('before append:', new_list) # new_list.append(x + y) # print('after append:', new_list) # a list comprehension to build the same list would look like this # un-comment and run when ready # new_list2 = [x + y for x, y in zip(a, b)] # the lists we built are the same # un-comment these lines when ready # print('new_list:', new_list) # print('new_list2:', new_list2)
1 Like