I’ve a function which aim is to find numbers that you cannot pair with others. If a number cannot be paired up with another identical number, then you will have to return it.
example:
[1,1,1,1, 2, 3, 4]
expected result:
[2,3,4]
def pair(input):
while len(input) > 2:
for i in range(len(input) - 1):
for j in range(len(input) - 1):
print(input)
if input[i] == input[j]:
input.pop(i)
input.pop(j)
print(input)
else:
break
return input
print(pair([1,1,1,1, 2, 3, 4]))
>Solution :
One way to solve this problem is to find the unique elements in the list, and if an element occurs an odd number of times the in the list, returned it.
def pair(lst):
set_lst = set(lst)
output = []
for elem in set_lst:
if lst.count(elem) % 2 != 0:
output.append(elem)
return output
print(pair([1, 1, 1, 1, 2, 3, 4]))