There are various ways in which you can get unique values from a list in Python.
Let's take a look at a few examples.
Example 1: Using set
number_list = [4, 2, 1, 4, 3, 2]
unique_values = list(set(number_list))
print(unique_values)
Output:
In the above example, we converted the list to a set and then back to a list to get the list of unique numbers.
Note: The output is a order list of unique values.
Example 2: Using a list comprehension
number_list = [4, 2, 1, 4, 3, 2]
unique_values = [no for i, no in enumerate(number_list) if no not in number_list[:i]]
print(unique_values)
Output:
Note: This will provide a list that is in order and has unique values.
Example 3: Using dict.fromkeys
number_list = [4, 2, 1, 4, 3, 2]
unique_values = list(dict.fromkeys(number_list))
print(unique_values)
Output:
Note: This will provide a list that is in order and has unique values.
Example 4: Using collections module
from collections import Counter
city_list = [
"Tokyo", "London", "New York", "Paris", "Sydney",
"Cairo", "Tokyo", "Beijing", "New York", "Moscow",
"Rio de Janeiro", "London", "Sydney", "Mumbai", "Lima"
]
city_counts = Counter(city_list)
unique_cities = [city for city, count in city_counts.items() if count == 1]
print(unique_cities)
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Python Switch-Case Statement equivalent like Java Example - Python
- Make Bootstrap Button look like a link - Bootstrap
- List of 28 Protocols supported by cURL with Examples - cURL
- What is Bootstrap Jumbotron and how to use it - Bootstrap
- Eclipse version 32-bit or 64-bit check on macOS - Eclipse
- How to Make Microsoft Excel Default Application on Mac - MacOS
- Remove duplicate lines using Notepad++ - NotepadPlusPlus
- How to crop a screenshot on Mac/Macbook - MacOS