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

2022-01-26 Thread Nick Farrell
>>> a = {1, 2, 3} >>> b = {2, 3, 4} >>> a & b {2, 3} On Wednesday, 26 January 2022 at 23:52:05 UTC+11 xaadx...@gmail.com 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 wrote: > >>

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

2022-01-26 Thread Muhammad Shehzad
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 wrote: > Use intersection > > On Wed, Jan 26, 2022, 4:06 PM bnmng wrote: > >> Thank you. I think I'll go with sets as advised, although this method >>

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

2022-01-26 Thread Muhammad Shehzad
Use intersection On Wed, Jan 26, 2022, 4:06 PM bnmng 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

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

2022-01-26 Thread bnmng
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:

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

2022-01-25 Thread Sam Chaffy
You can use sets On Tue, Jan 25, 2022 at 7:47 AM 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: >

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

2022-01-25 Thread bnmng
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

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

2022-01-25 Thread Kasper Laudrup
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:

Is there a function that returns common elements in multiple lists

2022-01-25 Thread bnmng
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 =