Beginner Here! I came across some python code about the zip() function being combined with the sum() function, but the code does not make sense to me and I was wondering if I could get an explanation:
list_1 = ['a', 'a', 'a', 'b']
list_2 = ['a', 'b', 'b', 'b', 'c']
print(sum(a != b for a, b in zip(list_1, list_2)))
a and b are not defined, but are being compared? Is it also looping through "a" with b for a
? What is a
and b
in this case? How are they being added together with sum()
? What is being looped through? If I can have some help understanding this, it would be greatly appreciated.
Thanks in advance!
When confronted with code like this, it's helpful to break it into bite-sized pieces and see what each does. Here's an annotated version:
list_1 = ['a', 'a', 'a', 'b']
list_2 = ['a', 'b', 'b', 'b', 'c']
print(list(zip(list_1, list_2))) # you need to pass this to list() because zip is a lazy iterator
# correponding pairs from each list
# zip() trucates to the shortest list, so `c` is ignored
# [('a', 'a'), ('a', 'b'), ('a', 'b'), ('b', 'b')]
print([(a, b) for a, b in zip(list_1, list_2)])
# same thing as above using a list comprehension
# loops over each pair in the zip and makes a tuple of (a,b)
print([a != b for a, b in zip(list_1, list_2)])
# [False, True, True, False]
# compare each item in those pairs. Are they different?
print(sum(a != b for a, b in zip(list_1, list_2)))
# 2
# take advantage of the fact that True = 1 and False = 0
# and sum those up -> 0 + 1 + 1 + 0
It's also helpful for lookup things like zip()
, and list comprehensions, although for many it makes more sense when you see them in action.