4. Data Structures (list, dict, tuples, sets, strings)ΒΆ
There are quite a few data structures available. The builtins data structures are: lists, tuples, dictionaries, strings, sets and frozensets.
Lists, strings and tuples are ordered sequences of objects. Unlike strings that contain only characters, list and tuples can contain any type of objects. Lists and tuples are like arrays. Tuples like strings are immutables. Lists are mutables so they can be extended or reduced at will. Sets are mutable unordered sequence of unique elements whereas frozensets are immutable sets.
Lists are enclosed in brackets:
l = [1, 2, "a"]
Tuples are enclosed in parentheses:
t = (1, 2, "a")
Tuples are faster and consume less memory. See Tuples for more information.
Dictionaries are built with curly brackets:
d = {"a":1, "b":2}
Sets are made using the set() builtin function. More about the data structures here below:
There are additional data structures available in the collections and heapq modules for instance.