#swe #flashcards #sets

Related: Software engineering | 8-15-2025 Dictionaries | 8-14-2025 Tuples | 1-15-2026 Sets lecture 1 and 2 |


Set

A set is a mutable and unordered collection of hashable objects. Set members must be distinct. They can hold multiple different data types and even nested structures like a tuple of tuples — as long as all elements can be hashed. Sets also come in an immutable frozensets flavor. sets do not support:- Indexing of any kind,

Set utility

Sets are most commonly used to quickly remove duplicates from other data structures or item groupings. They are also used for efficient comparisons when sequencing and duplicate tracking are not needed.

Sets vs other data structures

Like other collection types (dictionaries, lists, tuples), sets support: -
Iteration via for item in <set>
Membership checking via in and not in,
Length calculation through len(), and
Shallow copies through copy()
Sets do not support:
Indexing of any kind
Ordering via sorting or insertion
Slicing
Concatenation via +

Set time complexity

Checking membership in a set has constant time complexity (on average) versus checking membership in a list or string, where the time complexity grows as the length of the data increases. Methods such as <set>.union()<set>.intersection(), or <set>.difference() also have constant time complexity (on average).

Set literals

set can be directly entered as a set literal with curly {} brackets and commas between elements. Duplicates are silently omitted:

one_element = {'😀'} {'😀'}
multiple_elements = {'😀', '😃', '😄', '😁'} {'😀', '😃', '😄', '😁'}
multiple_duplicates = {'Hello!', 'Hello!', 'Hello!',
print(one_element)
print(multiple_elements)
print(multiple_duplicates)