0

I would like to get a list a=['wA', 'xB', 'yC', 'zD'], what argument to provide in commented section?

    i=None
    j=None
    a=[]

    for i in "wxyz":
        for j in "ABCD":
            while i!=j: # <-- here
                break
                a.append(i+j)

Thanks!

Suvo
  • 67
  • 5
  • 5
    `[i+j for i, j in zip('wxyz', 'ABCD')]` – user3483203 Jul 27 '18 at 14:51
  • 1
    Why do you need a `while` loop ? We could give you a solution to this but I'm not sure it's what you need. Maybe try to understand what each step means in your code first. – madjaoue Jul 27 '18 at 14:52
  • 1
    I don't understand why you add a while nested in your nested for loops, it's rather redundant and I think you meant to use an if block. That being said the append will never happen as it's in the same block as the while and the first line in the while is a break. – scrappedcola Jul 27 '18 at 14:52

2 Answers2

1

Your current loop will give you far more values than you want, because it will pair each letter from one set with each letter from the other. What you want to use is zip to combine the two strings of equal length:

i = 'wxyz'
j = 'ABCD'
a = [''.join(pair) for pair in zip(i,j)]
jack6e
  • 1,512
  • 10
  • 12
0

Why use a nested for loop? Since "wxyz" and "ABCD" have the same length, you can use a single loop to index over the indices of these:

str1 = 'wxyz'
str2 = 'ABCD'
mylist = []
for i in range(len(str1)): # Or range(len(str2)) since same length
    mylist.append(str1[i] + str2[i])
print(mylist) # ['wA', 'xB', 'yC', 'zD']
Joel
  • 1,564
  • 7
  • 12
  • 20