Created
December 26, 2018 22:06
-
-
Save bbookman/aca5b890a1f5ad64487594780301f82f to your computer and use it in GitHub Desktop.
Python List Comprehension: Find common numbers in two lists of numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Find the common numbers in two lists (without using a tuple or set) list_a = [1, 2, 3, 4], list_b = [2, 3, 4, 5] | |
''' | |
list_a = [1, 2, 3, 4] | |
list_b = [2, 3, 4, 5] | |
common = [a for a in list_a if a in list_b] | |
print(common) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
using nested loops
list_a = 1, 2, 3, 4
list_b = 2, 3, 4, 5
my_list = [element_b for element_b in list_b for element_a in list_a if element_b == element_a]
print(my_list)