How to use the Zip function in Python Part 3

Aaron S
Analytics Vidhya
5 min readJan 25, 2020

--

In the last article, we talked about the basics of the zip function and delved into an example. In this article, we go through two more examples of the zip function in real scripts. We will also learn about writing CSV files and touch on the pandas dataframe.

Example 2

When creating a script for films data I wanted to be able to write rows into a CSV file. Please see RealPython’s great article on writing CSV files here before reading on!

We use the CSV package to write rows one at a time. This is much easier if we zipped the data we want so each tuple represents the rows of data we want to input.

Here is the code below

import csv
with open('list.csv', 'w') as film:
writer = csv.writer(film)
writer.writerow(['Title','Date','link','imdb_link','synopsis'])
zip = [l for l in zip(title,dates,links, new_imdb2,news)]
for l in zip:
writer.writerow(l)
print('Successful grab')

Let’s go through this line by line!

import csv
with open('list.csv', 'w', newline='') as film:

We import the CSV package that comes with python. The ‘with’ statement opens the file name list.csv. Notice inside the brackets newline=’’, this ensures that data writes in a…

--

--

Aaron S
Analytics Vidhya

Hello! My name is Aaron. Healthcare professional with an interest in python, technology, education and healthcare.I run coding-medic.com for python enthusiasts.