>>> a = {1, 2, 3}
>>> b = {2, 3, 4}
>>> a & b
{2, 3}
On Wednesday, 26 January 2022 at 23:52:05 UTC+11 [email protected] wrote:> a = [1,2,3,4] > b = [3,4,5,6] > > Convert list a to set > a_set = set(a) > print(a_set.intersection(b)) > > On Wed, Jan 26, 2022, 5:47 PM Muhammad Shehzad <[email protected]> wrote: > >> Use intersection >> >> On Wed, Jan 26, 2022, 4:06 PM bnmng <[email protected]> wrote: >> >>> 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 [email protected] >>> wrote: >>> >>>> >>>> >>>> On Tue, Jan 25, 2022 at 3:07 PM bnmng <[email protected]> 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 [email protected]. >>>>> >>>> To view this discussion on the web visit >>>>> https://groups.google.com/d/msgid/django-users/4465553d-8aa1-4ab2-9e9c-19c4117d623fn%40googlegroups.com >>>>> >>>>> <https://groups.google.com/d/msgid/django-users/4465553d-8aa1-4ab2-9e9c-19c4117d623fn%40googlegroups.com?utm_medium=email&utm_source=footer> >>>>> . >>>>> >>>> -- >>>> 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 [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/fc7b49a3-2ca2-48e1-8556-f742e16fa4c9n%40googlegroups.com >>> >>> <https://groups.google.com/d/msgid/django-users/fc7b49a3-2ca2-48e1-8556-f742e16fa4c9n%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/58190fe5-8839-4698-8db2-a20c0a928ab3n%40googlegroups.com.

