2

I have two python lists. For example:

a = ['1', '2', '3', '4']
b =['1,2', '3,4', '3,33,34', '44']

I need to compare whether list[0] which is one is in b[0] which is 1, 2 and it has to return the output as 1 if it is present and 0 if it is not present.

The final output should be like:

1 (as 1 is present in 1,2)    
0 (as 2 is not present in 3,4)    
1 (as 3 is present in 3,33,34)    
0 (as 4 is not present in 44)

Please do help me in writing a code for this in python as I'm a beginner in this.

2 Answers 2

1

You can also try the following:

for index, value in enumerate(a):  
    if value in b[index].split(","):  
        print(1)  
    else:  
        print(0)
Sign up to request clarification or add additional context in comments.

4 Comments

no bro its checking digit by digit and showing the output. I mean for example '4' in '44' is shown as true instead of false. Thanks for trying to help me bro
@Yadhu Thanks I have debugged the code. Could you please intend my answer?
thanks bro this worked well and good and bro how do I make the result tat i got in here again back to list? like ['1', '0', '1' ,'0']
You can use a list to append the values instead of printing. Before the for loop begins make an empty list named output= [], and add output.append(1) and output.append(0) in place of print(1) and print(0) respectively. Hope it helps you brother
1

Use zip

Ex:

a = ['1','2','3','4']
b =['1,2', '3,4', '3,33,34', '44']

for i, v in zip(a, b):
    if i in v.split(","):     #Check if element in b 
        print(1)
    else:
        print(0)

Output:

1
0
1
0

2 Comments

Thanks bro, Thanks a lot this worked like a pro and jiffy.
Oops. Sorry i dint know that as I'm new here too . Done and thanks once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.