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?
Asked
Active
Viewed 44 times
-2
-
It's just a practice similar to that in languages like C ++ – امیرحمزه باقری May 28 '20 at 13:31
-
I want to write a class in Python to do very large numbers using strings. – امیرحمزه باقری May 28 '20 at 13:33
-
In this exercise, I have a problem with how to write the algorithm – امیرحمزه باقری May 28 '20 at 13:34
-
Ok, again, show us what you tried and where it went wrong. Are you getting errors? Your output is not what you expect? Have a look at [ask] and give us a [mre] – Tomerikoo May 28 '20 at 13:34
1 Answers
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