Reputation: 71
I'm trying to get a personalised message for each name corresponding to a mail defined by a list(a know how to get around this problem by using dictionary but dont know about lists).Can anyone help me by telling me how its done?
mail_id=['asd@g','nsg@g','psg@g']
names_list=['asd','nsg','psg']
for names in names_list:
for mail in mail_id:
msg='{}\n{}'.format(names,mail)
print(msg)
the output which this gives is:
asd
asd@g
asd
nsg@g
asd
psg@g
nsg
asd@g
nsg
nsg@g
nsg
psg@g
psg
asd@g
psg
nsg@g
psg
psg@g
The desired output:
asd
asd@g
nsg
nsg@g
psg
psg@g
here once again we can use indices to get the desired output but it'll be long and tedious.
Upvotes: 1
Views: 67
Reputation: 17322
if you like one line solution you could use a generator expression:
print(*('\n'.join(t) for t in zip(names_list, mail_id)), sep='\n')
output:
asd
asd@g
nsg
nsg@g
psg
psg@g
Upvotes: 1
Reputation: 695
If they're guaranteed to be in the same order you can just use the following:
mail_id=['asd@g','nsg@g','psg@g']
names_list=['asd','nsg','psg']
for index, name in enumerate(names_list):
print(name)
print(mail_id[index])
Hope this helps!!
Upvotes: 2
Reputation: 521
You can use zip and enumerate to get the output.
for i, j in enumerate (zip(mail_id,names_list)):
print (j[1])
print (j[0])
asd
asd@g
nsg
nsg@g
psg
psg@g
Upvotes: 2
Reputation: 20659
You can use zip
here.
for name,mail in zip(names_list,mail_id):
print(f'{name}\n{mail}')
asd
asd@g
nsg
nsg@g
psg
psg@g
Upvotes: 3