Tuesday, January 25, 2022

Re: Is there a function that returns common elements in multiple lists

Thank you.  That set.intersection with list conversion seems like the way to go.  On your second point, well taken.  I'll keep that in mind for future Python questions

On Tuesday, January 25, 2022 at 7:46:58 AM UTC-5 bnmng wrote:
Hello,

Is there a built in function that compares two or more lists and returns elements common to all?

like this:
 
def in_both( list_a, list_b ):
list_c=[]
for value in list_a:
if value in list_b:
list_c.append(value)
return list_c

Or this:
def in_all(list_of_lists):
list_a = list_of_lists.pop(0)
list_b = []
for value in list_a:
in_all_lists = True
for each_list in list_of_lists:
if not value in each_list:
in_all_lists = False
if in_all_lists:
list_b.append(value)
return list_b

Thank you

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4465553d-8aa1-4ab2-9e9c-19c4117d623fn%40googlegroups.com.

No comments:

Post a Comment