Count items common to both the lists in Python

A list is an ordered data structure with elements separated by a comma and enclosed within square brackets

List:

Lists are used to store multiple items in a single variable.

Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are TupleSet, and Dictionary, all with different qualities and usage.

Example:

a list = ["shaun", "melendez", "lea"]
print(a list)

Output:

[‘shaun’, ‘melendez’, ‘lea’]

The main motto of the topic is to find the common elements in the list .

we can write the code using and (&) function .

# initialize lists
a_list1 = [5, 6, 10, 9, 7, 1, 6]
b_list2 = [8, 6, 10, 3, 7, 10, 19]

# using set intersection to get number of identical elements
result = len(set(a_list1) & set(b_list2))

# printing result
print("Summation of Identical elements : " + str(result))

Output:

 

Summation of Identical elements : 3

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top