Python Zip

To start we’re going to create a simple dataframe in python:

If you want to watch a YouTube video based around the tutorial there is one embedded slightly below.

Example 1

In this basic example, we have two lists. One for jersey numbers and another for quarterbacks. We will then zip these together in the final list.

  fb_jersey_numbers = [14, 60, 16]
  quarterbacks = ['Y.A. Tittle', 'Otto Graham', 'George Blanda']

The order matters, so make sure you pass in the list first you want to appear in the front of each tuple.

  zipped = zip(fb_jersey_numbers, quarterbacks)
  print(list(zipped))

[(14, ‘Y.A. Tittle’), (60, ‘Otto Graham’), (16, ‘George Blanda’)]

Example 2 - Length Mismatch

If the iterables have different lengths, zip() will stop creating tuples when the shortest iterable is exhausted.

Our list on baseball jersey numbers will have 3 examples whereas players will have 4.

  basseball_jersey_numbers = [44, 32, 24]
  players = ['Hank Aaron', 'Sandy Koufax', 'Willie Mays', 'Al Kaline',]
  zipped = zip(basseball_jersey_numbers, players)
  print(list(zipped))

[(44, ‘Hank Aaron’), (32, ‘Sandy Koufax’), (24, ‘Willie Mays’)]

Example 3 - Mismatch no Value

While the previous example cut out Al Kaline, we can have the mismatch still appear in the final result. We have to import in zip longest first.

  from itertools import zip_longest

We can define what the missing value(s) can be filled with. In this case though I only passed in None.

  zipped_short = zip_longest(basseball_jersey_numbers, players, fillvalue=None)
  print(list(zipped_short))

[(44, ‘Hank Aaron’), (32, ‘Sandy Koufax’), (24, ‘Willie Mays’), (None, ‘Al Kaline’)]

Example 4 - 3 iterables

While the past examples were based around 2 iterables, let’s take a look at an example with 3. In this one we will look at combining pitchers, teams, and strikeouts.

  pitchers = ['Tom Seaver', 'Nolan Ryan', 'Phil Niekro']
  teams = ['New York Mets', 'California Angels', 'Atlanta Braves']
  strikeouts = [243, 186, 195]
  zipped = zip(pitchers, teams, strikeouts)
  print(list(zipped))

[(‘Tom Seaver’, ‘New York Mets’, 243), (‘Nolan Ryan’, ‘California Angels’, 186), (‘Phil Niekro’, ‘Atlanta Braves’, 195)]

Example 5 - Zip for Loop

Zip is quite common to use within for loops (any by extension list/dictionary comprehension). Let’s look at an example where we create a new list called total_contributions which is the sum of points and assists.

  points = [30, 24, 35]
  assists = [8, 12, 10]
  total_contribution = []

The For loop uses both pts for points list and ast for assists list. We use zip and then append the result of adding pts + ast.

   for pts, ast in zip(points, assists): total_contribution.append(pts + ast)
  print(total_contribution)

[38, 36, 45]

Example 6 - List Comprehension

We can also look at element wise min or max. This is probably easiest to accomplish by using zip inside list comprehension.

  seaver_strikeouts = [251, 201, 243]
  ryan_strikeouts = [383, 367, 186]
  min_strikeouts = [min(seaver, ryan) for seaver, ryan in zip(seaver_strikeouts, ryan_strikeouts)]
  print(min_strikeouts)

[251, 201, 186]

Example 7 - Dictionary Creation

We can also use zip to create a dictionary from two lists. In this case we will build out a dictionary based around the legendary cricketer Shane Warne. We have keys of name, wickets, and number. The values list has all the information.
  keys = ['name', 'wickets', 'number']
  values = ['Shane Warne', 708, 23]

We zip the two lists together and then use that as the parameter for dict()

  warne_dict = dict(zip(keys, values))
  print(warne_dict)

{‘name’: ‘Shane Warne’, ‘wickets’: 708, ‘number’: 23}

Example 8 - Interleaving lists

Interleaving lists are when you mix both lists into one final list. But instead of concatenating them, they are mixed together.
  numbers = [10, 18, 23]
  players = ['Sachin Tendulkar', 'Virat Kohli', 'Shane Warne']
  interleaved = [item for pair in zip(numbers, players) for item in pair]
  print(interleaved)

[10, ‘Sachin Tendulkar’, 18, ‘Virat Kohli’, 23, ‘Shane Warne’]

Example 9 - Modify Elements

Inside zip you can also modify elements. Let’s add 10 each element in strikeouts through list comprehension.

  names = ['Bob Lemon', 'Mickey McDermott', 'Earl Wilson']
  strikeouts = [162, 155, 149]
   for name, strikeout in zip(names, [k + 10 for k in strikeouts]): print(f"{name} now has {strikeout} strikeouts")

Bob Lemon now has 172 strikeouts
Mickey McDermott now has 165 strikeouts
Earl Wilson now has 159 strikeouts

Example 10 - Unzip

While this is a zip tutorial, might as well showcase what Unzip does. This is the opposite of zip as we remove the paired tuples within the list.

  zipped = [('Aaron Judge', 52), ('Giancarlo Stanton', 59), ('Joey Votto', 36)]
  players, home_runs = zip(*zipped)
  print(players)

(‘Aaron Judge’, ‘Giancarlo Stanton’, ‘Joey Votto’)

  print(home_runs)

(52, 59, 36)

Ryan is a Data Scientist at a fintech company, where he focuses on fraud prevention in underwriting and risk. Before that, he worked as a Data Analyst at a tax software company. He holds a degree in Electrical Engineering from UCF.

Leave a Reply

Your email address will not be published. Required fields are marked *