Function: length

length is a function defined in fns.c.

Signature

(length SEQUENCE)

Documentation

Return the length of vector, list or string SEQUENCE.

A byte-code function object is also allowed.

If the string contains multibyte characters, this is not necessarily the number of bytes in the string; it is the number of characters. To get the number of bytes, use string-bytes.

If the length of a list is being computed to compare to a (small) number, the length<, length> and length= functions may be more efficient.

Other relevant functions are documented in the vector, list and string groups.

View in manual

Probably introduced at or before Emacs version 20.1.

Shortdoc

;; string
(length "foo")
    => 3
  (length "avocado: 🥑")
    => 10
;; list
(length '(a b c))
    => 3
;; vector
(length [1 2 3])
    => 3

Aliases

set:size

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  EMACS_INT val;

  if (STRINGP (sequence))
    val = SCHARS (sequence);
  else if (CONSP (sequence))
    val = list_length (sequence);
  else if (NILP (sequence))
    val = 0;
  else if (VECTORP (sequence))
    val = ASIZE (sequence);
  else if (CHAR_TABLE_P (sequence))
    val = MAX_CHAR + 1;
  else if (BOOL_VECTOR_P (sequence))
    val = bool_vector_size (sequence);
  else if (CLOSUREP (sequence) || RECORDP (sequence))
    val = PVSIZE (sequence);
  else
    wrong_type_argument (Qsequencep, sequence);

  return make_fixnum (val);
}