Is there a function that returns common elements in multiple lists

53 views
Skip to first unread message

bnmng

unread,
Jan 25, 2022, 7:46:58 AM1/25/22
to Django users
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

Kasper Laudrup

unread,
Jan 25, 2022, 2:10:48 PM1/25/22
to django...@googlegroups.com
On 25/01/2022 13.46, bnmng wrote:
> Hello,
>
> Is there a built in function that compares two or more lists and returns
> elements common to all?
>

The intersect member function almost does that, just combine in with
converting your list to a set:

https://datagy.io/python-intersection-between-lists/

BTW it is better to ask questions like these on mailing lists dedicated
to Python programming in general.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

bnmng

unread,
Jan 25, 2022, 3:07:04 PM1/25/22
to Django users
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

Sam Chaffy

unread,
Jan 25, 2022, 3:08:59 PM1/25/22
to django...@googlegroups.com
You can use sets 

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bf2cf505-6205-4fcf-affd-9a0a2f7e5e00n%40googlegroups.com.
--
Oussama Chafiqui

Sam Chaffy

unread,
Jan 25, 2022, 3:11:10 PM1/25/22
to django...@googlegroups.com


--
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

bnmng

unread,
Jan 26, 2022, 6:06:11 AM1/26/22
to Django users
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/ 

Muhammad Shehzad

unread,
Jan 26, 2022, 7:48:02 AM1/26/22
to django...@googlegroups.com

Muhammad Shehzad

unread,
Jan 26, 2022, 7:52:05 AM1/26/22
to django...@googlegroups.com
a = [1,2,3,4]
b = [3,4,5,6]

Convert list a to set
a_set = set(a)
print(a_set.intersection(b))

Nick Farrell

unread,
Jan 26, 2022, 5:28:06 PM1/26/22
to Django users
>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a & b
{2, 3}
Reply all
Reply to author
Forward
0 new messages