get all the unique values from an array exept for some specific values to not consider in python in python

To get all the unique values from an array except for some specific values which we don't want to consider, we can use the following approach in Python:

main.py
# Sample input array
input_array = [1, 2, 3, 4, 4, 5, 6, 7, 7, 8]

# Specific values to remove from the array
to_ignore = [4, 7]

# Get unique values from the array
unique_values = set(input_array)

# Remove the specific values
unique_values -= set(to_ignore)

# Convert the set back to a list if required
final_array = list(unique_values)

# Print the final array
print(final_array)
# Output: [1, 2, 3, 5, 6, 8]
413 chars
19 lines

In this approach, we first create a set of unique elements from the input array. Then, we subtract another set containing the values to ignore from the unique set. Finally, we convert the updated set back to a list. This method will give us all the unique values from an array except for the specific values we don't want to consider.

gistlibby LogSnag