Python – Having trouble with arranging a parallel list within a for loop based on user input

Question:

Slow beginner here. Trying to write a program that asks the user how many entries to make. From that number, it will then ask the user for their name and address to print in a parallel list. So far this is my code but I’m positive it isn’t correct:

namelist = []
addrlist = []

n = int(input("How many people?: "))


for i in range(n):
    namelist.append(input("Name?: "))
    addrlist.append(input("Address?: "))
    
print("{} lives in {}".format(namelist, addrlist))

I’ve tweaked it but my issue is either 1) it only prints the last iteration of the loop or 2) it prints with brackets with the elements out of order.

The end goal is something like this:

Input
How many people?: 3
Name?: Brandy
Address?: Queens, NY
Name?: Aura
Address?: Los Angeles, CA
Name?: Kendrick
Address?: Chicago, IL

Output
Brandy lives in: Queens, NY
Aura lives in: Los Angeles, CA
Kendrick lives in: Chicago, IL

Asked By: kerorogunso

||

Answers:

As suggested in the comments by @MichaelButscher, you’ll want to use the zip() function, which combines each element from parallel lists into a tuple, then returns a new list of those tuples:

namelist = []
addrlist = []

n = int(input("How many people?: "))

for i in range(n):
    namelist.append(input("Name?: "))
    addrlist.append(input("Address?: "))
    
for name, addr in zip(namelist, addrlist):
    print("{} lives in {}".format(name, addr))
Answered By: Michael M.