1
key = ['q','w','e','r','t','y','u','i','o','p']
alp = ['a','b','c','d','e','f','g','h','i','j']

in this case, how to print like this

qa, wb, ec, rd, te ..... etc...

i just thought

 for k in key:
    for a in alp:
       print(str(k) + str(a))

but it's wrong of course. it prints just qa qb qc qd .... wa wb wc wd .... etc

so i tried

for k in key[d]:
   for a in alp[d]:
      print(str(k) + str(a))
      d = d+1

but it give me only one

qa
E.Laemas Kim
  • 87
  • 1
  • 8

2 Answers2

4

You can not use a nested loop such that, because you need to concatenate the elements with same index.

Instead You can simply use zip within a list comprehension :

>>> print ','.join(''.join(k) for k in zip(key,alp))
qa,wb,ec,rd,te,yf,ug,ih,oi,pj

Or :

print [i+j for i,j in zip(key,alp)]

And if you'r list have a difference size you can use itertools.izip_longet :

>>> from itertools import izip_longest
>>> alp = ['a','b','c','d','e','f','g','h','i','j','ee','ff']
>>> list(izip_longest(alp,key))
[('a', 'q'), ('b', 'w'), ('c', 'e'), ('d', 'r'), ('e', 't'), ('f', 'y'), ('g', 'u'), ('h', 'i'), ('i', 'o'), ('j', 'p'), ('ee', None), ('ff', None)]
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0
x=len(key)
for d in xrange(0,x):
    print key[d]+alp[d]

Output:

qa
wb
ec
rd
te
yf
ug
ih
oi
pj
ForceBru
  • 43,482
  • 10
  • 63
  • 98