Getting the Length of an Array in Python

Getting the Length of an Array in Python thumbnail
Python lets you store collections of data in arrays or lists

The Python programming language allows you to store and access collections of data in either arrays or array-like structures called lists. Both lists and arrays are data types that allow you to contain a set of data elements under a single variable name. Python programmers use lists rather than arrays because of their simplicity and flexibility. Lists can contain any Python data type, but arrays are designed to hold numeric data of a predefined type. The “array” data class must also be imported from the "array" module. The list data type and its functions, however, are built-in. The function "len()" can be used to find the length or number of elements contained in either lists or arrays.

Things You'll Need

  • Python interpreter version 2.6 or above installed
Show More

Instructions

  1. Using The Array Class

    • 1

      Click on the "Start" button. Click the "Run" menu option. Type the word "command" into the input box labeled "Open:" then click "OK."

    • 2

      Type “C:\Python\python” then press the enter key to load the Python interpreter. Some Windows Python interpreter installers use a version-specific directory. For instance, if the version of Python on your system is 2.7, you start it by typing “C:\Python27\python” at the command prompt then pressing the “Enter” key.

    • 3

      Type "from array import array" at the Python prompt then press the "Enter" key.

    • 4

      Type "myarray=array('H', [1,2,3,4,5])" at the Python prompt then press the "Enter" key.

    • 5

      Type "len(myarray)" at the Python command prompt then press the "Enter". The Python interpreter should print "5."

    Using Lists as Arrays

    • 6

      Click on the "Start" button. Click the "Run" menu option. Type the word "command" into the input box labeled "Open:" then click "OK."

    • 7

      Type “C:\Python\python” then press the enter key to load the Python interpreter.

    • 8

      Type "myarray=['a','b','c','d','e']" at the Python prompt then press the "Enter" key.

    • 9

      Type "len(myarray)" at the Python command prompt then press the “Enter” key. The Python interpreter should print "5."

    • 10

      Type "myarray +=myarray" at the Python prompt then press the "Enter" key. Type "len(myarray)" at the Python command prompt then press enter. The Python interpreter should now print "10" for the length of the list (array).

Tips & Warnings

  • Add arrays and array data handling functions to the Python language by installing the third-party module Numpy. While Numpy was created to perform mathematical and scientific calculations, its functions and data types can be used for any programming task that must process collections of data. Numpy is a free, open-source library you can download from the "Scipy" or "Python.org" websites.

  • Examine your list or array closely to ensure the functions you use to access them will find the data types they are expecting. Unlike arrays in C, C++ or Visual Basic, Python lists are not strongly typed. You can mix several data types in a single list. For example, myarray could be written "myarray=["cat",1, 2.5, "a"]" and still be a valid Python expression. If your function is expecting only numbers, it may throw an error exception once it accesses the list element, "cat."

Related Searches:

References

Resources

  • Photo Credit John Foxx/Stockbyte/Getty Images

Comments

Related Ads

Featured