What are some alternatives to writing

len(mylist3 & mylist4)

within this code?

mylist1 = [1,2,3]
mylist2 = [4,5,6]
mylist3 = set(mylist1)
mylist4 = set(mylist2)
lenresult = len(mylist3 & mylist4)

A module i'm importing is unable to use the count() that gets called with the ' & ' operator.

Recommended Answers

All 2 Replies

mylist1 = [1,2,3,4]
mylist2 = [3,4,5,6]

myset1 = set(mylist1)
myset2 = set(mylist2)

# intersection of sets myset1 and myset2
# gives a new set containing the common elements
myset3 = myset1.intersection(myset2)
print(myset3)       # set([3, 4]) 
print(len(myset3))  # 2

perfect, thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.