'zip function in python for lists
If I use the zip function than I am not getting the desired output. I want to combine two lists which are as follows:- list1 = [] list2 = [0]
I want the output as [0]. If I am using zip(list1,list2) I am getting [] as output.
I don't want to use any method other than zip. Is there any way I could get the output using zip function?
Solution 1:[1]
The issue is that zip is not used to achieve merging your lists
Here is the correct implementation
list1 = []
list2 = [0]
result=list1+list2
print(result)
# result output: [0]
However, zip function is used as an iterator between lists. below is an example to help you with the concept
a = ("1", "2", "3")
b = ("A", "B", "C")
x = zip(a, b)
for i in x: # will loop through the items in x
print(i)
# result output:
# ('1', 'A')
# ('2', 'B')
# ('3', 'C')
Solution 2:[2]
the zip method is not ment for that. just use the addition or the extend method
a=[0,1]
b=[2,3]
c = a+b
c
>>> [0, 1, 2, 3]
a.extend([2,3])
a
>>> [0, 1, 2, 3]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Youstanzr |
| Solution 2 | Marcelo |
