Adding .append to a list before zip

So I’m working on using the “.append” function in Python.
I have zipped 3 lists together to create a zipped list. I’m wondering if it is possible to append an item to one of the individual 3 lists I have zipped and have it print in my zipped list.

For Instance:
list1 = [1, 2, 3]
list2 = [‘A’, ‘B’, 'C,]
list3 = [4, 5, 6]
list3.append(7)

list4 = list(zip(list1, list2, list3))
print(list4)

Will list3.append(7) ever show up in my list4?

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/create-python-list/exercises/append-list

From the documentation:

Returns an iterator of tuples, where the i -th tuple contains the i -th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

https://docs.python.org/3/library/functions.html#zip

Your list3 contains the element you added, but it won’t be added to list4 unless you add an element to each of the other lists first.

Hope this helps!

Off topic

This was not expected…

z = zip([1,2,3],[4,5,6],[7,8,9])
a, b, c = [], [], []
for u in z:
  x, y, z = u
  a += [x]
  b += [y]
  c += [z]
print (a, b, c)
[1, 2, 3] [4, 5, 6] [7, 8, 9]
1 Like

Cool. I added a couple of print()'s to see what happens to z:

z = zip([1,2,3],[4,5,6],[7,8,9])
print(z)
a, b, c = [], [], []
for u in z:
  x, y, z = u
  a += [x]
  b += [y]
  c += [z]
print (a, b, c)
print(z)
<zip object at 0x7f0f72f96788>
[1, 2, 3] [4, 5, 6] [7, 8, 9]
9

Why are we not getting the expected result,

[1, 4,7] [2, 5, 8] [3, 6, 9]

?

[1, 2, 3] [4, 5, 6] [7, 8, 9] is the expected result The zip function creates 3 tuples from the 3 lists. The first tuple is (1, 4, 7), second is (2, 5, 8) and third is (3, 6, 9), so the first value of u is (1, 4, 7).
You unpack the tuple into x, y, z, so x = 1, y = 4 and z = 7. Then 1 is appended to a, 4 to b and 7 to c. Process repeats with next value of u => (2, 5, 8), and then (3, 6, 9).
The output you were expecting can be produced with simply:

z = zip([1,2,3],[4,5,6],[7,8,9])
a, b, c = z
print (a, b, c)
#or
print (list(a), list(b), list(c))
(1, 4, 7) (2, 5, 8) (3, 6, 9)
[1, 4, 7] [2, 5, 8] [3, 6, 9]
1 Like

D’oh! I can see that now; essentially repackaging in the same order as original lists. Had I printed u in my example I would have seen that right away. Hope you’re not laughing too hard!

>>> def zip_rip(z):
      a, b, c = z
      return list(a), list(b), list(c)


>>> print (zip_rip(zip([1,2,3],[4,5,6],[7,8,9])))
([1, 4, 7], [2, 5, 8], [3, 6, 9])
>>> 
1 Like

No. Not laughing. I figured you’d realize that your for loop was unzipping.
If I had a dime for every time I overlooked something obvious, I’d have a lot of dimes.:slightly_smiling_face:
I still find the behavior of z curious. It’s interesting that the second and third iterations of the loop access an ‘untagged’ object since z has been reassigned. I tried to mimic this in JavaScript, and it behaves the same way. Obviously once the for loop is interpreted the memory location is used without needing its ‘tag’.
Here’s my JavaScript test:

let z = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
let x
let y

const a = []
const b = []
const c = []

z.forEach(u => {
  x = u[0]
  y = u[1]
  z = u[2]
  a.push(x)
  b.push(y)
  c.push(z)
})

console.log(`a = ${a}\nb = ${b}\nc = ${c}`)
console.log(`z = ${z}`)
a = 1,4,7
b = 2,5,8
c = 3,6,9
z = 9

Just hadn’t really considered this before.

1 Like

Using let inside the block protects the integrity of z.

> z
<- (3) [Array(3), Array(3), Array(3)]

Yeah, I was just testing to see if the behavior would be the same in JavaScript as it is in Python where the .forEach iteration would continue even with z reassigned within the loop.

What is the question that this illuminates?

Isn’t the z in print(z) just the final value appended to the tuple x,y,z, having nothing to do with the zipped object z that is the iterator within for - in.

a = [1,2,3,4]
print(a)
for item in a:
    a = item
    print(a)
print(a)

Output:

[1, 2, 3, 4]
1
2
3
4
4

The value of z is re-assigned from the zipped object to the last element of the first tuple inside the for loop. I thought that it was curious that in the subsequent iterations even though z no longer points to the zipped object, u is assigned the next tuple. I was expecting a type error since z is no longer iterable.

I suspect that what is happening is the iterator is cached so not affected by the loop assignment. It is garbage collected at the end, while z still has its last assignment.

1 Like

I don’t think that z (the zipped object) is in play once for-in is called, since (as I understand itl) for - in actually creates a new iterable object to work with.

Edit - On second (or third) thought, an hour later … but then again, the original iterator is exhausted after the loop completes, so not that isolated after all. I obviously need to research it further.

1 Like