0

I have 2 lists

lis1=[1,2,3,4,5,6,2,3,1]
lis2=[4,5,8,7,10,6,9,8]

I want to write a function that can return elements that are present in either lis1 or lis2 the output should be [1,2,3,7,8,9,10] since 1,2,3 are not present in lis2 and 7,8,9,10 are not present in lis1

Glen Veigas
  • 85
  • 1
  • 12

2 Answers2

8

If you don't need ordered result, try this:

lis1 = [1, 2, 3, 4, 5, 6, 2, 3, 1]
lis2 = [4, 5, 8, 7, 10, 6, 9, 8]

set1 = set(lis1)
set2 = set(lis2)

print(set1 ^ set2)  # XOR operation for two sets

output:

{1, 2, 3, 7, 8, 9, 10}
Boseong Choi
  • 2,566
  • 9
  • 22
1

If you need to get an ordered result:

lis1=[1,2,3,4,5,6,2,3,1] lis2=[4,5,8,7,10,6,9,8]

y=11 put in y maximum number you want to check

for x in range(1, y): if x not in lis1 and x in lis2: print(x) if x not in lis2 and x in lis1: print(x)

Noah Shay
  • 44
  • 1
  • 4