hi guys
wat if List is equivalent 2 Arrays in c
or do we have Arrays seperately defined in python??

Dani AI

Generated

Short answer: lists are not the same as C-style arrays — they're a flexible, general-purpose container, while Python also offers several array-like options when you need compact, typed, or numeric storage.

As observed, Python lists are the usual go-to: they can hold mixed types and grow/shrink easily. In CPython a list is implemented as a dynamic array of pointers with an over-allocation/resizing strategy (fast indexing, amortized fast append) but significant per-item overhead because each element is a full Python object. (chromium.googlesource.com)

For compact, homogeneous storage the standard-library module array gives you a C-style typed sequence (signed/unsigned ints, floats, etc.) with lower per-element memory cost and contiguous storage — useful when you need a lightweight numeric or byte buffer without NumPy. Note that historical typecodes differed between Python 2 and Python 3 (some character-type codes that existed in 2.x are not the same in 3.x), so check the current array docs for your Python version. (docs.python.org)

If you need numeric work, multi-dimensional arrays, vectorized math or linear algebra (determinants, inverses, eigenvalues), use NumPy’s ndarray — it’s built for that workload and interoperates with BLAS/LAPACK via numpy.linalg. Lists-of-lists work for tiny matrices but are awkward and slow for real numeric computation. (numpy.org)

Quick practical tip: to compare memory use, measure container overhead and raw buffer bytes — sys.getsizeof() for Python objects and ndarray.nbytes for NumPy data buffers — and interpret both results together. Example (run in your interpreter to compare on your platform):

import sys, array
import numpy as np

L = list(range(10000))
A = array.array('i', L)
N = np.array(L)

print('list', sys.getsizeof(L))
print('array', sys.getsizeof(A))
print('ndarray bytes', N.nbytes)

sys.getsizeof and ndarray.nbytes report different things; use both to understand overhead vs. element storage. (docs.python.org)

Summary: use lists for mixed, flexible data; array for compact homogeneous buffers; NumPy for numeric, multi-dimensional, high-performance math. and pointed to the right alternatives — pick the one that matches your data shape and performance needs.

Recommended Answers

All 5 Replies

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:
    print(n)


# get more information on module array
help('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.

commented: You knowledge of Python is astounding +5

Erm, oops?

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]
3

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.

Jeff

thanx ene and jrcagle...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.