python - Intersection of lists, look for on length -


if looking intersection of 2 lists,

b1 = set(alist).intersection(alist2) print(b1) 

however if want intersection of 2 lists of words of length 4 only.

for example.

so if

alist = ["james", "kobe", "ball"] 

and

alist2 = ["jimmy","james","kobe"] 

i expect b1 = ["kobe"] because kobe of length 4 while james not

do need take out words of length 4 list first ? or there way check while doing intersection ?

you can use comprehension this:

four_letter_intersection = {     word word in set(list_a).intersection(list_b) if len(word) == 4     } 

Popular posts from this blog