-1
x=[1,3,5,8]
y=[2,4,5,8]

z=[5,8]

how do I combine lists x and y to a list z, where only values are given, which are present in x AND y?

Thanks for your help

2 Answers2

1

You could do something like

x = [1, 3, 5, 8]
y = [2, 4, 5, 8]

z = [k for k in y if k in x]
0

here is the solution-

# Initialisation of first list 
list1 = [1,3,5,8] 

# Initialisation of Second list 
list2 = [2,4,5,8]

output = [i for i in list2 if i in list1]

# printing result 
print(output) 
Pankaj Mishra
  • 445
  • 6
  • 15