2

Possible Duplicate:
how can I iterate through two lists in parallel in Python?

i have 3 lists like this:

name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
country = ['USA', 'England', 'France', 'Japan', 'China']
age = [23, 41, 19, 16, 55]

and i want an output like:

sam; USA; 23
john; England; 41
napoleon; France; 19
tanaka; Japan; 16
ming; China; 55

any help would be great. thanks in advance

Community
  • 1
  • 1
H.Choi
  • 3,095
  • 7
  • 26
  • 24

5 Answers5

10

you can use zip():

name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
country = ['USA', 'England', 'France', 'Japan', 'China']
age = [23, 41, 19, 16, 55]
for x,y,z in zip(name,country,age):
    print("{0}; {1}; {2}".format(x,y,z))

or use map():

mapp=map(lambda x,y,z:"; ".join((x,y,str(z))),name,country,age)
for x in mapp:
    print(x)

output:

sam; USA; 23
john; England; 41
napoleon; France; 19
tanaka; Japan; 16
ming; China; 55
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
3
name = "sam john napoleon tanaka ming".split()
country = "USA England France Japan China".split()
age = [23, 41, 19, 16, 55]

print "\n".join(["%s; %s %d" % (x) for x in zip(name,country,age)])
Maria Zverina
  • 10,863
  • 3
  • 44
  • 61
  • +1 for one liner, use `format()` instead of these old `%s` formatting. – Ashwini Chaudhary Jul 12 '12 at 16:29
  • 1
    @AshwiniChaudhary actually `%s` is still totally credable – Jakob Bowyer Jul 12 '12 at 16:30
  • @JakobBowyer but it's good to use `format()` as the old style formatting might get deprecated. – Ashwini Chaudhary Jul 12 '12 at 16:33
  • @AshwiniChaudhary might is a big thing in language design – Jakob Bowyer Jul 12 '12 at 19:17
  • @JakobBowyer PEP 3101: Advanced String Formatting. Note: the 2.6 description `mentions the format() method for both 8-bit and Unicode strings. In 3.0, only the str type (text strings with Unicode support) supports this method; the bytes type does not. The plan is to eventually make this the only API for string formatting, and to start deprecating the % operator in Python 3.1.` source :[What’s New In Python 3.0](http://docs.python.org/release/3.0.1/whatsnew/3.0.html), so it's better if we suggest using `format`. – Ashwini Chaudhary Jul 12 '12 at 19:23
  • Its still gonna be around for a long long time – Jakob Bowyer Jul 12 '12 at 19:31
1

Assuming the lists are the same length:

for i in range(len(name)):
    print '%s; %s; %s' %(name[i], country[i], age[i])

UPDATE: This assumes you're using Python 3.x. For 2.x, use xrange for general performance improvement. Alternatively:

for i in range(len(name)):
    print '{0}; {1}; {2}'.format{name[i], country[i], age[i]}
TakeS
  • 616
  • 1
  • 7
  • 14
0

If you know they will all have the same length, then you can do:

name = ['sam', 'john', 'napoleon', 'tanaka', 'ming']
country = ['USA', 'England', 'France', 'Japan', 'China']
age = [23, 41, 19, 16, 55]

i = 0

for n in name:
    print name[i] + '; ' + country[i] + '; ' + str(age[i])
    i++
sth
  • 222,467
  • 53
  • 283
  • 367
EnriqueC91
  • 23
  • 5
0

My 2 cents ;-)

Using a generator from the itertools module:

from itertools import izip

nlist = ['%s; %s; %s' %t for t in izip(name, country, age)]
Don Question
  • 11,227
  • 5
  • 36
  • 54