ScanSkill

len

This len() function returns an integer type that specifies the number of elements in an object(collection or sequence).

Syntax

len(collection)
len(sequence)

Here,

  • collection: Optional. List, tuple, string, byte, range, etc.
  • sequence: Optional. Dictionary, set, or frozenset.

Examples

  • With set, frozenset, and dictionary
>>> # with set
>>> len({'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'})
8
>>> set1 = {'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'}
>>> len(set1)
8

>>> # with dictionary
>>> dict1 = {'a':1, 'b': 2, 'c':3, 'd':4}
>>> len(dict1)
4

>>> # with frozenset
>>> set1 = {'o', 'x', 'f', 'y', 'u', 'C', 'd', 'l'}
>>> frozenset1 = frozenset(set1)
>>> len(frozenset1)
8
  • With string, list, tuple, range…
>>> # with string
>>> string1 = "cloudyfox"
>>> len(string1)
9

>>> # with list
>>> list1 = [1, 2, 3]
>>> len(list1)
3

>>> # with tuple
>>> tuple1 = (1, 2, 3)
>>> len(tuple1)
3

>>> # with range
>>> range1 = range(1, 5)
>>> len(range1)
4