-3

I wish for my program to search resident and if it a specific value it will print the same placed data from other lists. For example in this situation I would want it to print;

"Name is Alan, age is 7 and they do not live in place",

as well as,

"Name is Margaret, age is 66 and they do not live in place"

name = ["Alan", "Susan", "Margaret"]
age = [7, 34, 66]
resident = [0, 1, 0]

if resident = 0:
    print ("name is {}, age is {} and they do not live in place".format(name[], age[]))
  • [Pythonic iteration over multiple lists in parallel](http://stackoverflow.com/q/21911483/2301450) – vaultah Apr 03 '16 at 08:20

3 Answers3

0
for person, ages, residency in zip(name,age,resident):
    if residency == 0:
        print("name is {0}, age is {1} and they do not live in place".format(person, ages))

zip takes multiple lists and returns a list of tuples. Here's an example:

zip(name, age, resident)
>>>> [('Alan', 7, 0), ('Susan', 34, 1), ('Margaret', 66, 0)]

Iterating over each tuple is then very easy.

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
0

Consider also the use of enumerate, which will provide the index and item from the iterated list:

>>> for i,x in enumerate(resident):
        if x == 0:
            print("name is {0}, age is {1} and they do not live in place".format(name[i], age[i]))


name is Alan, age is 7 and they do not live in place
name is Margaret, age is 66 and they do not live in place
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
-1

You can use the commonly used %s string replacement:

name = ["Alan", "Susan", "Margaret"]
age = [7, 34, 66]
resident = [0, 1, 0]

if resident[0] == 0:print ("name is %s, age is %s and they do not live in place"%(name[0],age[0]))
jhoepken
  • 1,842
  • 3
  • 17
  • 24
Mdemir
  • 1
  • 1