8 Years
Discussion Span
Last Post by karthik.c

We don't have separately defined arrays. Lists are the closest thing we got, I believe.

There is a library called NumPy that implements C arrays for python. The only limitation ( I think ) is that they must be arrays of numbers.


Oh yes, Python has an array type, but you must import module array:

# module array has Python's array type

import array

# Python25 use 'c' as the character typecode
# Python30 uses unicode character typecode 'u'
char_array = array.array('c', 'hello world')

print(char_array[0])    # h
print(char_array)       # array('c', 'hello world')

# 'i' uses a signed integer of 2 bytes
int_array = array.array('i', [2, 4, -11])

print(int_array[2])     # -11
print(int_array)        # array('i', [2, 4, -11])

# 'f' uses 4 bytes and 'd' uses 8 bytes per element 
float_array = array.array('d', [1.0, 2.0, 3.14])

print(float_array[2])  # 3.14

for n in float_array:

# get more information on module array

Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. The type is specified at object creation time. Arrays use up less space in memory, for a limited number of applications this is important.

Votes + Comments
You knowledge of Python is astounding

One more thought: lists of lists in Python function reasonably well as arrays:

mylist = [[1,2,3],[4,5,6],[7,8,9]]

print mylist[0][2]

I say "reasonably well" -- there aren't any linear algebra methods that can be used on lists of lists (like det(), inv(), or eigenvalues()), but the LinearAlgebra module covers that.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.