How to get rid of comma at the end of printing from loop

So basically I have the list of many points and I want to extract only unique values.
I have written a function but I have 1 problem: how to avoid printing comma at the end of the list?

def unique(list1):
    unique_values = []
    for u in list1:
        if u not in unique_values:
            unique_values.append(u)
    for u in unique_values:
        print(u, end=", ")


wells = ["U1", "U1", "U3", "U3", "U3", "U5", "U5", "U5", "U7", "U7", "U7", "U7", "U7", "U8", "U8"]
print("The unique values from list are...:", end=" ")
unique(wells)

my output is for now: "The unique values from list are…: U1, U3, U5, U7, U8,"

>Solution :

It might be an overkill but you can use NumPy "unique" method, which is probably more efficient, a specially for large arrays or long lists.

The following code will do:

import numpy as np
x = np.array(['a', 'a', 'b', 'c', 'd', 'd'])
y = np.unique(x)
print(', '.join(y))

and the result is:

a, b, c, d

Leave a Reply