Home > Enterprise >  Add two list index wise in python but list 1 has a value list 2 has no value means it needs to displ
Add two list index wise in python but list 1 has a value list 2 has no value means it needs to displ

Time:03-31

The question was merge two list index wise eg:

a = [ab,bc,cd]
b = []

expected output:

ans = [ab,bc,cd]

I have tried this

a = input("enter the list 1:")
list1 = a.split(",")
b = input("enter the list 2:")
list2 = b.split(",")

if list2 == EMPTY_LIST:
    print(list1)
else:
    res = [i j for i, j in zip(list1,list2)] 
    print("Ans: ",res)

but the output is

enter the list 1:abi,ajith
enter the list 2:
Ans:  ['abi'] 

expected output i need

enter the list 1:abi,ajith
enter the list 2:
Ans:  ['abi','ajith'] 

CodePudding user response:

There are several issues here. First of all, input always returns a string. If you didn't type anything before hitting the return, it will be an empty string. Therefore, in your program,

b = ''  # an empty string

The split function always returns a list of strings. If called on the empty string, it returns a list containing one element, the empty string. Therefore, in your program,

list2 = ['']

This is not an empty list. It contains 1 element. If you use it a boolean expression, its value is True because it contains a nonzero number of elements. Therefore:

bool(list2) = True

and so your if expression if not list2 fails. The code takes the else branch, as you can see because your output begins with the text "Ans:".

Now the zip function terminates as soon as ONE of its sequences ends. list1 contains 2 elements but list2 contains only one, so only 1 iteration occurs. Only one element gets printed. That explains exactly what you see.

Programming requires you to think about and understand every detail.

CodePudding user response:

Try the following please:

a = input("enter the list 1:")
list1 = a.split(",")
b = input("enter the list 2:")
list2 = b.split(",")

if not list2:
    print(list1)
else:
    res = [_ for _ in (list1 list2) if len(_)]
    print("Ans: ",res)

CodePudding user response:

You were checking an empty list the wrong way. Since the user did not enter anything, you cannot check if the list is empty or not. Or I should say that list will not be empty since it will be equal to ['']. You need to check if the input is empty.

a = input("enter the list 1:")
list1 = a.split(",")
print(list1)
b = input("enter the list 2:")
list2 = b.split(",")

if not b: # This is the way of checking if an input is empty or not
    print(list1)
else:
    res = [i j for i, j in zip(list1,list2)] 
    print("Ans: ",res)

CodePudding user response:

When you use input, it returns a string, even if you don't pass any value in it. So it will return '' so list2 will not be empty.

You can fix it by checking if '' is not the list and also list lenght is 1:

if len(list2) ==1 and '' in list2 :
    print(list1)
else:
    res = [i j for i, j in zip(list1,list2)] 
    print("Ans: ",res)

CodePudding user response:

You should probably also handle the case where list2 is not empty but does not have the same number of elements as list1

If you pad the list with empty strings, you can then zip them without issue

n=max(len(list1),len(list2))
for i in range(n):
   if i >= len(list1): a.append("")
   if i >= len(list2): b.append("")

res = [i j for i, j in zip(list1,list2)]

Or perhaps a simpler way:

import itertools as it
list1 = ["ab","bc","cd"]
list2 = []

[i j for i, j in it.zip_longest(list1, list2, fillvalue="")]

CodePudding user response:

Try this

if list2 == []:
    print(list1)
  • Related