Skip to content

Instantly share code, notes, and snippets.

@sidisinsane
Last active July 16, 2023 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sidisinsane/44e5c37a2d41d19e185b9cbfbef68890 to your computer and use it in GitHub Desktop.
Save sidisinsane/44e5c37a2d41d19e185b9cbfbef68890 to your computer and use it in GitHub Desktop.
Return a list containing unique values from a given array.
import numpy as np
def list_unique(arr):
"""
Return a list containing unique values from a given array.
Args:
arr (list or numpy.ndarray): The input list.
Returns:
list: A list containing unique values.
Example:
>>> arr = [1, 2, 3, 2, 4, 1, 5]
>>> lst_unique = list_unique(arr)
>>> lst_json = json.dumps(lst_unique, indent=2, ensure_ascii=False)
>>> print(lst_json)
[
1,
2,
3,
4,
5
]
"""
return np.unique(np.array(arr)).tolist()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment