Python Practice › List Programs › Program 17

Do two lists share a member?

EasyList Programs · 17 of 100

Write a Python program that returns True if two lists have at least one common member.

Advertisement
Input
[1, 2, 3] and [5, 3, 9]; [1, 2] and [7, 8]
Output
True False

Approach

  1. Two sets are disjoint when they share nothing.

Solution

Python
def has_common(a, b):    return not set(a).isdisjoint(b) print(has_common([1, 2, 3], [5, 3, 9]), has_common([1, 2], [7, 8]))
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Leave a Comment