states = [ #a list of strings "Alabama", "Alaska", "Arizona", "Arkansas", "California" ] capitals = [ "Montgomery", #Alabama "Juneau", #Alaska "Phoenix", #Arizona "Little Rock", #Arkansas "Sacramento" #California ] assert len(states) == len(capitals), "The lists must be of equal length." #Does the same thing as the assert statement. #if len(states) != len(capitals): # raise AssertionError("The lists must be of equal length.") for i in range(len(states)): print(f"{states[i]:10} {capitals[i]}") print() for state, capital in zip(states, capitals): print(f"{state:10} {capital}")
Alabama Montgomery Alaska Juneau Arizona Phoenix Arkansas Little Rock California Sacramento Alabama Montgomery Alaska Juneau Arizona Phoenix Arkansas Little Rock California Sacramento
Of course,
it would have been better to make a
list
of
list
s
to avoid the need for a
zip
:
states = [ ["Alabama", "Montgomery"], ["Alaska", "Juneau"], ["Arizona", "Phoenix"], ["Arkansas", "Little Rock"], ["California", "Sacramento"] ] for state in states: print(f"{state[0]:10} {state[1]}")
Alabama Montgomery Alaska Juneau Arizona Phoenix Arkansas Little Rock California Sacramento
states = [ #a list of strings "Alabama", "Alaska", "Arizona", "Arkansas", "California" ] capitals = [ "Montgomery", #Alabama "Juneau", #Alaska "Phoenix", #Arizona "Little Rock", #Arkansas "Sacramento" #California ] nicknames = [ "The Yellowhammer State", #Alabama "The Last Frontier", #Alaska" "The Grand Canyon State", #Arizona" "The Natural State", #Arkansas" "The Golden State" #California ] assert len(states) == len(capitals) == len(nicknames), "The lists must be of equal length." for i in range(len(states)): print(f"{states[i]:10} {capitals[i]:11} {nicknames[i]}") print() for state, capital, nickname in zip(states, capitals, nicknames): print(f"{state:10} {capital:11} {nickname}")
Alabama Montgomery The Yellowhammer State Alaska Juneau The Last Frontier Arizona Phoenix The Grand Canyon State Arkansas Little Rock The Natural State California Sacramento The Golden State Alabama Montgomery The Yellowhammer State Alaska Juneau The Last Frontier Arizona Phoenix The Grand Canyon State Arkansas Little Rock The Natural State California Sacramento The Golden State
zip
must be
iterable
objects.
(See
For.)
Three examples of iterable objects are
the following
list
of
str
ings,
string
of characters,
and
range
of
int
s.
lastNames = [ #a list of 5 strings "Bush", #2000 "Bush", #2004 "Obama", #2008 "Obama", #2012 "Trump" #2016 ] firstInitials = "GGBBD" #a string of 5 characters years = range(2000, 2020, 4) #a range of 5 ints assert len(lastNames) == len(firstInitials) == len(years), \ "The iterables must be of equal length." for lastName, firstInitial, year in zip(lastNames, firstInitials, years): print(f"{year} {firstInitial}. {lastName}")
2000 G. Bush 2004 G. Bush 2008 B. Obama 2012 B. Obama 2016 D. Trump
itertools.zip_longest
instead of plain old
zip
.
import itertools pleasures = [ "new car", "vacation in Florida", "greasy chow fun in a basement dive on Mott Street", "hot dog in Central Park", "view from the top of the Palisades", "a baby's smile" ] prices = [ 35_000.00, 3_000.00, 14.00, 2.75 ] for pleasure, price in itertools.zip_longest(pleasures, prices, fillvalue = 0.00): print(f"${price:9,.2f} {pleasure}")
$35,000.00 new car $ 3,000.00 vacation in Florida $ 14.00 greasy chow fun in a basement dive on Mott Street $ 2.75 hot dog in Central Park $ 0.00 view from the top of the Palisades $ 0.00 a baby's smile
for
loop that loops through the return value of a
zip
:
states = [ "Alabama", "Alaska", "Arizona", ] capitals = [ "Montgomery", #Alabama "Juneau", #Alaska "Phoenix", #Arizona ] for state, capital in zip(states, capitals): print(f"{state:10} {capital}")
Alabama Montgomery Alaska Juneau Arizona Phoenix
But we can also have one variable.
The one variable will be a
tuple
.
states = [ "Alabama", "Alaska", "Arizona", ] capitals = [ "Montgomery", #Alabama "Juneau", #Alaska "Phoenix", #Arizona ] for t in zip(states, capitals): #t is a tuple containing two strings. print(f"{t[0]:10} {t[1]}")
Alabama Montgomery Alaska Juneau Arizona Phoenix
zip
receives
numberOfColumns
arguments.
Each of these arguments is a
list
of
height
int
s.
""" Print a long list in columns. """ import sys n = 100 longList = range(n) numberOfColumns = 5 height = n // numberOfColumns #of each column #listOfColumns will be a list of numberOfColumns smaller lists. #Each smaller list will contain height ints. listOfColumns = [] for i in range(numberOfColumns): column = longList[i * height: (i + 1) * height] listOfColumns.append(column) for t in zip(*listOfColumns): #t is a tuple containing numberOfColumns ints. for i, number in enumerate(t): #Since t is a tuple, we can loop through it. print(f"{number:2}", end = "") if i == numberOfColumns - 1: print() else: print(5 * " ", end = "") sys.exit(0)
0 20 40 60 80 1 21 41 61 81 2 22 42 62 82 3 23 43 63 83 4 24 44 64 84 5 25 45 65 85 6 26 46 66 86 7 27 47 67 87 8 28 48 68 88 9 29 49 69 89 10 30 50 70 90 11 31 51 71 91 12 32 52 72 92 13 33 53 73 93 14 34 54 74 94 15 35 55 75 95 16 36 56 76 96 17 37 57 77 97 18 38 58 78 98 19 39 59 79 99
We can create the
listOfColumns
more simply with a
list
comprehension:
listOfColumns = [longList[i * height: (i + 1) * height] for i in range(numberOfColumns)]
import sys people = [ ["John", "Doe"], ["Mary", "Jones"], ["Mike", "Smith"] ] reflected = zip(*people) #Pass 3 arguments to zip. for row in reflected: for name in row: print(f"{name:5}", end = " ") print() sys.exit(0)
reflected
contains two rows and three columns.
"John"
is still in the upper right corner,
"Smith"
is still in the lower left corner:
John Mary Mike Doe Jones Smith