Home > Enterprise >  zip function in python for lists
zip function in python for lists

Time:03-23

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?

CodePudding user response:

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')

CodePudding user response:

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]
  • Related