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.