-2

How do I combine the first member of a list in Python with the first member of the second list as well as the second member of the first list with the second member of the second list? And will this happen until the last members of the lists go ahead?

1 Answers1

1

This is a basic zip function. For example:

list_1 = [1, 2, 3]
list_2 = ["s", "u", "p"]

final_list = list(zip(list_1, list_2))
print(final_list)

This will return a list of tuples such that the first value from the first list is paired with the first value of the second list, iterating over each value up until the minimum length between the two lists is satisfied. In the example, this would be the output: [(1, 's'), (2, 'u'), (3, 'p')]

It's important to note that if one of the lists is longer than the other, the extra values will be excluded. There are a ton of ways to handle this, but it would help to know what you're wanting to accomplish.

patmcb
  • 443
  • 2
  • 9