Syntax ⧉ Copy t = ( val1, val2, ...)
t = val1, val2Example ⧉ Copy point = ( 10 , 20 )
singleton = ( 42 ,)
x, y = point
print ( f"x= { x} , y= { y} " )
print ( len ( singleton)) Output x=10, y=20
1Note A single-element tuple requires a trailing comma: (42,). Without it, (42) is just an integer in parentheses. Tuples are immutable.
create tuple single element tuple unpack tuple immutable sequence
Syntax ⧉ Copy from collections import namedtuple
Point = namedtuple ( 'Point' , [ 'x' , 'y' ]) Example ⧉ Copy from collections import namedtuple
Color = namedtuple ( "Color" , [ "red" , "green" , "blue" ])
sky = Color ( 135 , 206 , 235 )
print ( sky. red , sky. blue )
print ( sky. _asdict ()) Output 135 235
{'red': 135, 'green': 206, 'blue': 235}Note Named tuples give readable field access while staying immutable. For mutable fields or defaults, consider dataclasses instead.
NamedTuple (typing-based) Syntax ⧉ Copy from typing import NamedTuple
class Name ( NamedTuple ) :
field: type Example ⧉ Copy from typing import NamedTuple
class Endpoint ( NamedTuple ) :
host: str
port: int = 443
api = Endpoint ( "api.example.com" )
print ( api. host , api. port ) Output api.example.com 443Note The class-based NamedTuple supports type annotations and default values, making it more modern than the functional namedtuple() form.
typed named tuple namedtuple class NamedTuple defaults
Syntax ⧉ Copy s = { val1, val2, ...}
s = set ( iterable) Example ⧉ Copy 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.
Syntax ⧉ Copy a | b ( union)
a & b ( intersection)
a - b ( difference)
a ^ b ( symmetric difference) Example ⧉ Copy frontend = { "alice" , "bob" , "carol" }
backend = { "bob" , "carol" , "dave" }
print ( frontend | backend)
print ( frontend & backend)
print ( frontend - backend)
print ( frontend ^ backend) Output {'alice', 'bob', 'carol', 'dave'}
{'bob', 'carol'}
{'alice'}
{'alice', 'dave'}Note Set operations are extremely fast (O(min(len(a), len(b))) for intersection). Use them instead of nested loops for membership checks.
frozenset (Immutable Set) Syntax ⧉ Copy fs = frozenset ( iterable) Example ⧉ Copy permissions = frozenset ([ "read" , "write" ])
print ( "read" in permissions)
cache = { permissions: "admin" }
print ( cache[ frozenset ([ "read" , "write" ])]) Output True
adminNote frozenset is hashable, so it can be used as a dict key or as an element inside another set. Regular sets cannot.
frozenset immutable set hashable set set as key
Syntax ⧉ Copy { expression for item in iterable if condition} Example ⧉ Copy emails = [ "[email protected] " , "[email protected] " , "[email protected] " , "[email protected] " ]
unique_lower = { e. lower () for e in emails}
print ( unique_lower) Output {'[email protected] ', '[email protected] ', '[email protected] '}Note Set comprehensions automatically deduplicate results. Useful for extracting unique transformed values from a collection.
Syntax ⧉ Copy s. add ( elem)
s. discard ( elem)
s. remove ( elem) Example ⧉ Copy active = { "alice" , "bob" }
active. add ( "carol" )
active. discard ( "bob" )
active. discard ( "nonexistent" )
print ( active) Output {'alice', 'carol'}Note discard() silently ignores missing elements. remove() raises KeyError if the element is not found. Prefer discard() when absence is acceptable.
add to set remove from set discard set add