ruk·si

🐍 Python
Arrays

Updated at 2018-06-09 17:21

Arrays are ordered, mutable and typed sequences of data. They behave like lists but you restrict what can be added e.g. only strings, integers, floats or doubles.

from array import ArrayType, array

numbers: ArrayType = array('f', (1.0, 1.5, 2.0, 2.5))
assert numbers[1] == 1.5
del numbers[1]
assert numbers[1] == 2.0
numbers.append(3)
numbers.append(3.5)
# numbers.append('not-numbers') => TypeError

Arrays are better than lists when you are storing 10 million floating point values. But other that, arrays are rarely needed.

Source

  • Python Tricks The Book, Dan Bader
  • Fluent Python, Luciano Ramalho