-1

I have two lists,am trying to find the any element in lista is present in list,I know I can two forloops to do a match ,are there better ways to achieve this without two for loops

lista=['LA.BF.2.1']
listb=['LA.BF.2.1','LA.BF64.1.2.1','LA.BF64.1.1']
for element in lista:
    for element in listb:
                  match
  • @kasra - sorry I dont need intersection logic...its a match logic..updated question –  Jun 24 '15 at 18:15
  • 2
    Still *am trying to find the any element in lista is present in list,* means intersection – Mazdak Jun 24 '15 at 18:16

2 Answers2

0

Maybe using any

>>> lista=['LA.BF.2.1']
>>> listb=['LA.BF.2.1','LA.BF64.1.2.1','LA.BF64.1.1']
>>> any([ i in listb for i in lista])
True
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • @nullp - I need to know if there is a match for each i in lista,how to do that? –  Jun 24 '15 at 18:24
  • for data `lista=['LA.BF.2.1','LA.BF.2.2'] listb=['LA.BF.2.1','LA.BF64.1.2.1','LA.BF64.1.1']` ,I get the output as False,there is a match for LA.BF.2.1 –  Jun 24 '15 at 18:33
  • @PythonProg I think you are looking for `any` then, updated my answer – nu11p01n73R Jun 24 '15 at 19:22
0

If your aim is to find any element in lista that is also present in listb , you can convert the lists to set, and then do set.intersection .

Example -

>>> lista=['LA.BF.2.1','SOMETHINGELSE']
>>> listb=['LA.BF.2.1','LA.BF64.1.2.1','LA.BF64.1.1']
>>>
>>> list(set(lista).intersection(listb))
['LA.BF.2.1']
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • I want to see if there is a match or not for each element of lista in listb, either case I need a response of true or false,basically if there is a match or not.. –  Jun 24 '15 at 18:35
  • can you please define match? – Anand S Kumar Jun 24 '15 at 18:37
  • Do you mean if any one element in lista is in listb, then return true, else if all elements in lista are not in listb return false ? – Anand S Kumar Jun 24 '15 at 18:37