Unique values

3 snippets across 2 stacks - Python, SQL

PYPython

Creating Sets

PY · Tuples & Sets
Syntax
s = {val1, val2, ...}
s = set(iterable)
Example
tags = {"python", "tutorial", "beginner"}
from_list = set([1, 2, 2, 3, 3, 3])
print(from_list)

empty_set = set()
print(type(empty_set))
Output
{1, 2, 3}
<class 'set'>

Note Use set() for an empty set, NOT {}. Empty braces {} create an empty dictionary, not a set.

SQLSQL

DISTINCT

SQL · Basic Queries
Syntax
SELECT DISTINCT column FROM table;
Example
SELECT DISTINCT city
FROM users
ORDER BY city;
Output
-- Returns each city only once, no duplicates

Note DISTINCT applies to the entire row when used with multiple columns. SELECT DISTINCT city, state treats (city, state) pairs as the unit of uniqueness.

Frequently asked questions

How does Python handle unique values?
This task is covered in 2 stacks on this page: Python, SQL. The "Creating Sets" snippet in Python uses `s = {val1, val2, ...}`.
Which code does the Python example use?
The "Creating Sets" snippet uses `s = {val1, val2, ...}`, from the Tuples & Sets section of the Python cheat sheet.
Which stacks cover "unique values" on this page?
Python, SQL. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Creating Sets": Use set() for an empty set, NOT {}. Empty braces {} create an empty dictionary, not a set.