0

I have two lists:

one = ['a','b','c','d']
two = ['d', 'c',' b', 'a']

I would like to create a loop that produces the following output: [('a', d'), ('b', 'c'), ('c','b'), ('d', 'a')]

This is my current code but it is not properly adding the parentheses.

final_list = []
for i in range(len(a)):
    final_list.append(a[i])
    final_list.append(b[i])
6114617
  • 79
  • 2
  • 7

2 Answers2

1

Append tuples in the for loop:

for i in range(len(a)):
    final_list.append((a[i],b[i]))
not_speshal
  • 22,093
  • 2
  • 15
  • 30
1

You are looking for a list of tuples, zip will do it for you

final_list = [t for t in zip(one, two)]
# [('a', 'd'), ('b', 'c'), ('c', 'b'), ('d', 'a')]

Or as suggested you can also do it without the explicit loop

final_list = list(zip(one, two))
Guy
  • 46,488
  • 10
  • 44
  • 88