How do you find common values in a list?

Python | Print all the common elements of two lists

Given two lists, print all the common elements of two lists.

Examples:

Input : list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 9] Output : {5} Explanation: The common elements of both the lists are 3 and 4 Input : list1 = [1, 2, 3, 4, 5] list2 = [6, 7, 8, 9] Output : No common elements Explanation: They do not have any elements in common in between them

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1:Using Set’s & property

Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
Below is the Python3 implementation of the above approach:






# Python program to find the common elements

# in two lists

def common_member(a, b):

a_set = set(a)

b_set = set(b)

if (a_set & b_set):

print(a_set & b_set)

else:

print("No common elements")

a = [1, 2, 3, 4, 5]

b = [5, 6, 7, 8, 9]

common_member(a, b)

a = [1, 2, 3, 4, 5]

b = [6, 7, 8, 9]

common_member(a, b)

Output:

{5} No common elements

Method 2:Using Set’s intersection property

Convert the list to set by conversion. Use the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.
Below is the Python3 implementation of the above approach:




# Python program to find common elements in

# both sets using intersection function in

# sets

# function

def common_member(a, b):

a_set = set(a)

b_set = set(b)

# check length

if len(a_set.intersection(b_set)) > 0:

return(a_set.intersection(b_set))

else:

return("no common elements")

a = [1, 2, 3, 4, 5]

b = [5, 6, 7, 8, 9]

print(common_member(a, b))

a =[1, 2, 3, 4, 5]

b =[6, 7, 8, 9]

print(common_member(a, b))

Output:

{5} No common elements

Method 3 : Using for loop

def common_member(a, b): result = [i for i in a if i in b] return result a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] print("The common elements in the two lists are: ") print(common_member(a, b))

How do you find common values in a list?




Article Tags :

Python

Python list-programs

python-list

Practice Tags :

python-list

Find the Most Common Elements of a List in Python

Python Python List

Created: July-02, 2021 | Updated: October-21, 2021

This article mentions several ways to find the most common elements of a list in Python. The following are the functions that we can use to find the most common list elements in Python.

  • Use the most_common() function of Counter.
  • Use the max() function of FreqDist().
  • Use the unique()function of NumPy.

Extract common values from two lists

How do you find common values in a list?

Generic formula

=FILTER(list1,COUNTIF(list2,list1))

Summary

To compare two lists and extract common values, you can use a formula based on the FILTER and COUNTIF functions. In the example shown, the formula in F5 is:

=FILTER(list1,COUNTIF(list2,list1))

where list1 (B5:B15) and list2 (D5:D13) are named ranges. The result, values that appear in both lists, spills into the range F5:F11.

Explanation

The FILTER function accepts an array of values and an "include" argument which filters the array based on a logical expression or value.

In this case, the array is provided as the named range "list1", which contains all values in B5:B15. The include argument is delivered by the COUNTIF function, which is nested inside FILTER:

=FILTER(list1,COUNTIF(list2,list1))

COUNTIF is set up with list2 as range, and list1 as criteria. Because we give COUNTIF eleven criteria values, COUNTIF returns eleven results in an array like this:

{1;1;0;1;0;1;0;1;0;1;1}

Notice the 1's correspond to items in list2 that appear in list1.

This array is delivered directly to the FILTER function as the "include" argument:

=FILTER(list1,{1;1;0;1;0;1;0;1;0;1;1})

The FILTER function filters list1 using the values provided by COUNTIF. Values associated with zero are removed; other values are preserved.

The final result is an array of values that exist in both lists, which spills into the range F5:F11.

Extended logic

In the above formula, we use the raw results from COUNTIF as the filter. This works because Excel evaluates any non-zero value as TRUE, and zero as FALSE. If COUNTIF returns a count greater than 1, the filter will still work properly.

To force TRUE and FALSE results explicitly, you can use ">0" like this:

=FILTER(list1,COUNTIF(list2,list1)>0)

Remove duplicates or sort

To remove duplicates, just nest the formula inside the UNIQUE function:

=UNIQUE(FILTER(list1,COUNTIF(list2,list1)))

To sort results, nest in the SORT function:

=SORT(UNIQUE(FILTER(list1,COUNTIF(list2,list1))))

List values missing from list2

To output values in list1 missing from list2, you can reverse the logic like this:

=FILTER(list1,COUNTIF(list2,list1)=0)

Author

Dave Bruns

Related formulas

How do you find common values in a list?

Unique values

This example uses the UNIQUE function, which is fully automatic. When UNIQUE is provided with the range B5:B16, which contains 12 values, it returns the 7 unique values seen in D5:D11. UNIQUE is a dynamic function. If any data in B5:B16 changes, the...

How do you find common values in a list?

Distinct values

This example uses the UNIQUE function. With default settings, UNIQUE will output a list of unique values, i.e. values that appear one or more times in the source data. However, UNIQUE has an optional third argument, called "occurs_once" that, when...

How do you find common values in a list?

Unique values with criteria

This example uses the UNIQUE function together with the FILTER function. Working from the inside out, the FILTER function is first used to remove limit data to values associated with group A only: FILTER ( B5:B16 , C5:C16 = E4 ) Notice we are...

How do you find common values in a list?

Extract unique items from a list

The core of this formula is a basic lookup with INDEX: = INDEX ( list , row ) In other words, give INDEX the list and a row number, and INDEX will retrieve a value to add to the unique list. The hard work is figuring out the ROW number to give INDEX...

How do you find common values in a list?

Unique values ignore blanks

This example uses the UNIQUE function together with the FILTER function. Working from the inside out, the FILTER function is first used to remove any blank values from the data: FILTER ( B5:B16 , B5:B16 <> "" ) The symbol is a...

Related functions

How do you find common values in a list?

Excel FILTER Function

The Excel FILTER function filters a range of data based on supplied criteria, and extracts matching records.

How do you find common values in a list?

Excel COUNTIF Function

COUNTIF is an Excel function to count cells in a range that meet a single condition. COUNTIF can be used to count cells that contain dates, numbers, and text. The criteria used in COUNTIF supports logical...

How do you find common values in a list?

Excel UNIQUE Function

The Excel UNIQUE function returns a list of unique values in a list or range. Values can be text, numbers, dates, times, etc.

How do you find common values in a list?

Excel SORT Function

The Excel SORT function sorts the contents of a range or array in ascending or descending order. Values can be sorted by one or more columns. SORT returns a dynamic array of results.

See also

Dynamic array formulas in Excel

Alternatives to Dynamic Array Functions

Related courses

Dynamic Array Formulas

How do you find common values in a list?

Find common elements in list of lists in Python

PythonServer Side ProgrammingProgramming



It is possible to have a list whose inner elements are also lists. In such cases we may come across a need when we have to find out the common elements among these inner lists. In this article we will find out the approaches to achieve this.

Find the common elements in two lists in Python

By Shriprakash Tiwari

In this tutorial, We are going to learn how to find the common elements in two lists in Python.
To find the common list from two lists, we have to store the common elements in the third variable. There are various methods to find and store the common elements from two lists.

Using the intersection() Function

This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection() function is a built-in python function that is used to return a set that contains the elements which are common in two sets. The sets can be of any form i.e a list or a dictionary.

Example:

1

2

3

4

5

6

7

if __name__ == '__main__':

list_one = [5, 10, 15, 20, 25, 30]

list_two = [10, 20, 30, 40, 50, 60]

common_list = set(list_one).intersection(list_two)

print(common_list)

Output:

{10, 20, 30}

Note that, __name__ variable is used in the above code. It is a built-in variable that is used to see whether the current code is being run on its own or an external module is being imported from somewhere. Thus, the __name__ variable checks the name of the current module in the ongoing code. It is usually used with an if statement and where it is assigned to '__main__' when there is no module in the program. Otherwise, __name__ is assigned to the name of the module used in the program.