39

I want to declare an Array and all items present in the ListBox Should Be deleted irrespective of the Group name present in the ListBox. can any body help me coding in Python. I am using WINXP OS & Python 2.6.

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
Srinath
  • 431
  • 1
  • 4
  • 3

5 Answers5

103

In Python, a list is a dynamic array. You can create one like this:

lst = [] # Declares an empty list named lst

Or you can fill it with items:

lst = [1,2,3]

You can add items using "append":

lst.append('a')

You can iterate over elements of the list using the for loop:

for item in lst:
    # Do something with item

Or, if you'd like to keep track of the current index:

for idx, item in enumerate(lst):
    # idx is the current idx, while item is lst[idx]

To remove elements, you can use the del command or the remove function as in:

del lst[0] # Deletes the first item
lst.remove(x) # Removes the first occurence of x in the list

Note, though, that one cannot iterate over the list and modify it at the same time; to do that, you should instead iterate over a slice of the list (which is basically a copy of the list). As in:

 for item in lst[:]: # Notice the [:] which makes a slice
       # Now we can modify lst, since we are iterating over a copy of it
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
9

In python, A dynamic array is an 'array' from the array module. E.g.

from array import array
x = array('d')          #'d' denotes an array of type double
x.append(1.1)
x.append(2.2)
x.pop()                 # returns 2.2

This datatype is essentially a cross between the built-in 'list' type and the numpy 'ndarray' type. Like an ndarray, elements in arrays are C types, specified at initialization. They are not pointers to python objects; this may help avoid some misuse and semantic errors, and modestly improves performance.

However, this datatype has essentially the same methods as a python list, barring a few string & file conversion methods. It lacks all the extra numerical functionality of an ndarray.

See https://docs.python.org/2/library/array.html for details.

ChrisM
  • 572
  • 7
  • 7
  • Is the array module capable of holding 'string values', or just numerical ones. I camer across a table displaying: "array module - Efficient arrays of numeric values" The only viable C type was 'Py-Unicode', for a Unicode character. – gimmegimme Jun 22 '22 at 15:52
6

Here's a great method I recently found on a different stack overflow post regarding multi-dimensional arrays, but the answer works beautifully for single dimensional arrays as well:

# Create an 8 x 5 matrix of 0's:
w, h = 8, 5;
MyMatrix = [ [0 for x in range( w )] for y in range( h ) ]  

# Create an array of objects:
MyList = [ {} for x in range( n ) ]

I love this because you can specify the contents and size dynamically, in one line!

One more for the road:

# Dynamic content initialization:
MyFunkyArray = [ x * a + b for x in range ( n ) ]
SpaceHaze
  • 61
  • 1
  • 1
0

you can declare a Numpy array dynamically for 1 dimension as shown below:

import numpy as np

n = 2
new_table = np.empty(shape=[n,1])

new_table[0,0] = 2
new_table[1,0] = 3
print(new_table)

The above example assumes we know we need to have 1 column but we want to allocate the number of rows dynamically (in this case the number or rows required is equal to 2)

output is shown below:

[[2.] [3.]]

fuzzy
  • 1
  • 1
0

While a regular python list can be used as a dynamic list, one problem it may have is the way it is implemented. In CPython lists are implemented as an array of double pointers as explained here. Now if you want to iterate through a long list, there will be many jumps in memory and you will have less performance compared to an array that is stored "contiguously" in memory, meaning all the elements are stored one after another. If a list is sored in this way, because of Temporal Locality the CPU will spend less time accessing that memory.

So if you want your list to be stored this way, you can use numpy arrays. But numpy arrays don't implement Geometric Expansion. So you can create a class that does this in python. Every time we add an element, the list will get expanded to x times its size (in which x is implemented differently in different languages). This type of dynamic array will not be able to store different types of data like python lists (which in itself is not a good thing to do), but it will be faster to iterate over than python lists.

Implementing dynamic arrays using numpy has already been addressed here and in this repository.

Mohammad Alavi
  • 912
  • 1
  • 6
  • 16