Meaning
Returns the number of elements in a set.
Primary Function
Return the cardinality (size) of a set.
Communicative Purpose
Inquire about the size of a collection to inform control flow or reporting.
Pattern
len( <set_expression> )
Core Structure
len( container )
Função primária
Return the cardinality (size) of a set.
Propósito comunicativo
Inquire about the size of a collection to inform control flow or reporting.
Situações de gatilho
When checking if a set is empty, controlling loop iterations, or reporting collection size.
Contextos
Used in conditionals, loops, comprehensions, logging, and debugging statements.
Padrão
len( <set_expression> )
Estrutura central
len( container )
Slots de substituição
<set_expression>: any expression that evaluates to a set or other sized collection.
Colocados típicos
- if
- while
- for
- in
- not
- len
- ==
- >
- <
Substituições comuns
- len(my_list)
- len(my_string)
- len(my_dict)
Erros comuns
Applying len to non-sized objects (e.g., int), forgetting parentheses, confusing with attribute .size.
Similar / contraste
Len(my_set)lenlen(my_set) method, .size attribute (NumPy arrays), length property in other languages.
Interferências
Confusing len with length property in other languages; assuming len works on iterators without len(my_set)lenlen(my_set).
Família do chunk
- built-in functions
Nuance
For built-in set, len operates in O(1) time and returns an integer count of distinct elements.
Efeito pragmático
Used to make decisions based on collection size, often in guarding loops or reporting status.
Dica de memória
How many items are in my set?
Nota
len works on any object implementing len(my_set)lenlen(my_set); for set it is constant time.
Upgrade path
Use len on other collections; consider .size for libraries like NumPy.
Log in to save chunks.