Tho Le

Ph.D. candidate in EECS at Northwestern. Looking for a job in data science.

Python - How to zip and upzip

07 Apr 2017 » python, example
salaries_and_tenures = [(83000, 8.7), (88000, 8.1),
(48000, 0.7), (76000, 6),
(69000, 6.5), (76000, 7.5),
(60000, 2.5), (83000, 10),
(48000, 1.9), (63000, 4.2)]
x, y =zip(*salaries_and_tenures)#zip with * means un-zip

import matplotlib.pyplot as plt
%matplotlib inline
plt.scatter(x, y)
plt.show()

#try to zip the two list again
zipped = zip(y, x)
print(list(zipped))

#another way:
for salary, tenure in salaries_and_tenures:
    print(salary, tenure)

Original source: Grus-Data Science from scratch.

Related Posts