Wednesday, January 26, 2022

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

Thank you.  I think I'll go with sets as advised, although this method also looks very neat: 
intersection = [item for item in list1 if item in list2] found at https://datagy.io/python-intersection-between-lists/ 

 
  

On Tuesday, January 25, 2022 at 3:11:10 PM UTC-5 joezep...@gmail.com wrote:


On Tue, Jan 25, 2022 at 3:07 PM bnmng <benj...@bnmng.com> wrote:
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...@googlegroups.com.
--
Oussama Chafiqui

--
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/fc7b49a3-2ca2-48e1-8556-f742e16fa4c9n%40googlegroups.com.

No comments:

Post a Comment