Why do zip() lists have parenthesis instead of brackets around the zipped data?

What exactly is “zip(list1, list2)” doing to the data? I thought it was taking 2 lists, putting them together so that the new zipped list is a group of lists.

What do I mean?
I thought if you had the below lists

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]

and zipped them
newList = list(zip(list1, list2))

you would end up with a list of 4 lists
newList == [[1, 5], [2, 6], [3, 7], [4, 8]]

During the gradebook lesson I found out that you actually end up with the below
newList == [(1, 5), (2, 6), (3, 7), (4, 8)]

What is the difference between having the data in parenthesis instead of brackets in the zipped list?

More info - here is the code I used for the gradebook lesson

subjects = ['physics', 'calculus', 'poetry', 'history']
grades = [98, 97, 85, 88]
gradebook = list(zip(subjects, grades))

gradebook.append(('computer science', 100))
print(gradebook)

Results
[(‘physics’, 98), (‘calculus’, 97), (‘poetry’, 85), (‘history’, 88), (‘computer science’, 100)]

Here is the first code I used where I noticed the parenthesis in the list instead of brackets

subjects = ['physics', 'calculus', 'poetry', 'history']
grades = [98, 97, 85, 88]
gradebook = list(zip(subjects, grades))

gradebook.append(['computer science', 100])
print(gradebook)

Results
[(‘physics’, 98), (‘calculus’, 97), (‘poetry’, 85), (‘history’, 88), [‘computer science’, 100]]

While it is subtle, since both are sequences, the chief difference is mutability. A list is mutable, a tuple is not. zip objects are consumable and expected to keep the order (a, b) of each element.

We can use dict.update() to consume the zip object, thereby creating key/value pairs from the entries (assuming an a-b tuple).

>>> list1 = [1, 2, 3, 4]
>>> list2 = [5, 6, 7, 8]
>>> newDict = {}
>>> newDict.update(zip(list1, list2))

>>> newDict
{1: 5, 2: 6, 3: 7, 4: 8}
>>> 

Now porting this over to your last example, above,

>>> subjects = ['physics', 'calculus', 'poetry', 'history']
>>> grades = [98, 97, 85, 88]
>>> grade_book = {}
>>> grade_book.update(zip(subjects, grades))
>>> grade_book
{'history': 88, 'calculus': 97, 'poetry': 85, 'physics': 98}
>>> grade_book.update({'computer science': 100})
>>> grade_book
{'history': 88, 'computer science': 100, 'poetry': 85, 'physics': 98, 'calculus': 97}
>>> 

Notice we go straight from two individual lists to a dictionary in one go?


Note

The examples above are generated in 3.5.1. As I understand it, 3.6 or 3.7 make orderedDicts by default, so their order of insertion will match the original lists.

Confirmed with this repl

https://repl.it/@mtf/Two-lists-to-one-dictionary?language=python3

{'physics': 98, 'calculus': 97, 'poetry': 85, 'history': 88}
{'physics': 98, 'calculus': 97, 'poetry': 85, 'history': 88, 'computer science': 100}

Thanks for the very detailed description. I haven’t made it to the dictionary lessons yet, but am familiar with them and your explanation made perfect sense.

Edit: Just got through all the lists lessons and the Tuple video at the end explained this concept very well as well.

1 Like