The order of items in a combined list of lists

I’m working on lists and on exercise:

Python Gradebook

I combined two lists using the following piece of code:
full_gradebook = list(zip(gradebook, last_semester_gradebook))

Where,
last_semester_gradebook = [(“politics”, 80), (“latin”, 96), (“dance”, 97), (“architecture”, 65)]
gradebook = [(‘physics’, 98), (‘calculus’, 97), (‘poetry’, 85), (‘history’, 88), (‘computer science’, 100), (‘visual arts’, 93)]

Now, when I combine both:
full_gradebook = list(zip(gradebook, last_semester_gradebook))
I get the following output:
[((‘physics’, 98), (‘politics’, 80)), ((‘calculus’, 97), (‘latin’, 96)), ((‘poetry’, 85), (‘dance’, 97)), ((‘history’, 88), (‘architecture’, 65))]

Now, how are the items ordered when they are combined? why am I seeing a mix of both lists and not like an append to the existing list?

Hi @chharish. As far as I’m aware zip should maintain the order of the iterables you supply to it. I’m not sure what you mean by seeing a mix of both lists rather than an append, zip is used to create an iterator using elements from each iterator separately, connecting them by index. If you wanted to append to a given list then that’s what you would do instead.

If the given text is exactly how you typed this out then the behaviour may not be what you expect as you’ve wrapped a number of elements with parantheses creating tuples. Consequently the zeroth element of last_semester_gradebook is the tuple (“politics”, 80) and (“physics”, 98) for gradebook. The first element from iterating through zip would then be a tuple of these two…
((“politics”, 80), (“physics”, 98)).

I hope this helps but if it does not then you may need to explain your exact issue further. If you’re submitting further code to the forums please check this post-

2 Likes

zip isn’t supposed to append, its name is zip, not concat. might wanna read its documentation, what does it promise to do?

2 Likes

The exact issue is the one you’ve already identified - the OP is using zip() to accomplish a task for which zip() is not suited.

If I have a list some_list and I wish to add another list another_list to the end of it, I can simply concatenate the lists:

>>> some_list = ["mango","banana","apple"]
>>> another_list = ["cherry","guava","watermelon"]
>>> combined_list = some_list + another_list
>>> print(combined_list)
['mango', 'banana', 'apple', 'cherry', 'guava', 'watermelon']
>>>

Also, calling last_semester_gradebook = [(“politics”, 80), (“latin”, 96), (“dance”, 97), (“architecture”, 65)] a “list of lists” is a misnomer… as it’s a list of tuples, which are a wholly different type in Python.

1 Like

Thank you for being kind and responding.

The output is exactly how I saw it. I understand that each item in the list is a tuple,

Let’s say the first element from iterating through zip would result in adding both ((“politics”, 80), (“physics”, 98)) to the combined list. How does the second iteration take place next?

I was expecting to see (“latin”, 96), ‘calculus’, 97) for the second tuple but that was not the case.

The combined list doesn’t seem to be in any order at all. So literally, this means zip has no order in which it combines both the lists?

“List of tuples” , I will note this, also thanks for the concat (+), I use it for append, just trying to make sense of zip.

Not sure your observations quite add up here. I suggest showing code with this behaviour.

zip does this:
https://en.wikipedia.org/wiki/Transpose#/media/File:Matrix_transpose.gif

and like suggested before, reading its documentation will tell you what that order is

an additional suggestion is to implement it yourself. either as you imagine it to behave or as you observe it to behave. by implementing it you are required to process what it does, you may find that some things don’t make sense or need to be one way or another

1 Like

Also keep in mind that you have an extra level of nesting going on, since you’re starting out with tuples.
For the purpose of observing what’s going on, something simpler would be better. You can also use values that you can see where they come from so that you don’t need to cross-reference it to the definition of your two lists:

['a1', 'a2', ...]
['b1', 'b2', ...]
1 Like