I have to take first list element and check if that substring is present in the other list element. if the substring is present then append that string to another list.
INPUT list1 = ["2044","1222"] list2 = ["I am Raman 2044","I am Raman 2044"x,"I am Raman 2044","I am Rohan 1222","I am Rohan 1222"]
OUTPUT list3 = ["I am Raman 2044I am Raman 2044I am Raman 2044","I am Rohan 1222I am Rohan 1222"]
CodePudding user response:
to check if string is in a list you use a in list[]
in your case, you can use it in loop.
for i in range(len(list1)):
if list1[i] in list2:
list2.append(list1[i])
CodePudding user response:
k=[]
list1 = ["2044","1222"]
list2 = ["I am Raman 2044","I am Raman 2044","I am Raman 2044","I am Rohan 1222","I am Rohan 1222"]
Raman = ''.join(list2).split('I am Rohan 1222')
Rohan = ''.join(list2).split('I am Raman 2044')
for x in Raman:
if len(x)>0:
k.append(x)
for y in Rohan:
if len(y)>0:
k.append(y)
print(k)
['I am Raman 2044I am Raman 2044I am Raman 2044', 'I am Rohan 1222I am Rohan 1222']