Aniruddh
Aniruddh

Reputation: 188

Suppose i have two lists in python a list of first names and a list of last names

firstName = ['abcd','efghi','jkl','mnopqr']
lastName  = ['xyz','pqrst','uvw','klmn']

my desired output is:

abcd xyz [email protected]
efghi pqrst [email protected] 
jkl uvw [email protected] 
mnopqr klmn [email protected] 

I have tried various methods and failed. The closest I could come up with was:

for x,y in zip(firstName,lastName):
    print(x,y)

What should I do?

Upvotes: 0

Views: 939

Answers (5)

sisanared
sisanared

Reputation: 4287

Use basic string formatting and print the output like shown below:

for x, y in zip(firstName,lastName):
    print(x, y, "%s.%[email protected]"%(x[0], y))

Upvotes: 2

R.Sharp
R.Sharp

Reputation: 316

This prints the output you're looking for. Did you really just want to print it ?

for x,y in zip(firstName,lastName):
    print (x,y, x[0] + r'.' + y + r'@example.com')

Upvotes: 2

Benjamin
Benjamin

Reputation: 2296

for i,j in zip(firstName,lastName):
    print (i+" "+j+" "+i[0]+"."+j+"@example.com")

output

abcd xyz [email protected]
efghi pqrst [email protected]
jkl uvw [email protected]
mnopqr klmn [email protected]

Upvotes: 1

LukeBowl
LukeBowl

Reputation: 200

firstName = ['abcd','efghi','jkl','mnopqr']
lastName  = ['xyz','pqrst','uvw','klmn']

for index in range(0, len(firstName)):

    first = firstName[index]
    last = lastName[index]
    email = first[0] + '.' + last + '@example.com'

    print first, last, email

Upvotes: 1

Lokesh Sanapalli
Lokesh Sanapalli

Reputation: 1034

>>> for x,y in zip(firstName,lastName):
...     print("{0}\t{1}\t{2}".format(x,y,x[0]+'.'+y+'@example.com'))
... 
abcd    xyz [email protected]
efghi   pqrst   [email protected]
jkl uvw [email protected]
mnopqr  klmn    [email protected]

Upvotes: 2

Related Questions