Hi all,
I am new to python and am trying to understand the purpose of zip function. Could someone kindly give a real-life application of python’s zip function please?
Thank you very much!
Hi all,
I am new to python and am trying to understand the purpose of zip function. Could someone kindly give a real-life application of python’s zip function please?
Thank you very much!
The purpose is to produce an iterator from two or more lists, with tuples containing the corresponding elements of each list.
>>> coef = zip([0, 1, 2], [0, 1, 2], [0, 1, 2])
>>> x = 1
>>> for a, b, c in coef:
print (f"{a}x**2 + {b}x + {c} = {a*x**2 + b*x + c}")
0x**2 + 0x + 0 = 0
1x**2 + 1x + 1 = 3
2x**2 + 2x + 2 = 6
>>>
Note how much simpler it is to access all three lists at once, rather than fumbling with each separately?