Files
30-seconds-of-code/snippets/similarity.md
Isabelle Viktoria Maciohsek bff03eb7f4 Update snippet descriptions
2020-11-02 19:28:35 +02:00

309 B

title, tags
title tags
similarity list,beginner

Returns a list of elements that exist in both lists.

  • Use a list comprehension on a to only keep values contained in both lists.
def similarity(a, b):
  return [item for item in a if item in b]
similarity([1, 2, 3], [1, 2, 4]) # [1, 2]