06.0 Python‎ > ‎Python Notes‎ > ‎List Processing‎ > ‎

Lists as Arrays

Python does not have an intrinsic array Type, however, Python does have a list type.
Lists can be used to construct an array structure in Python.

 Python  Array in most other Languages         
arrays are not an intrinsic data type    arrays are intrinsic
lists are dynamic -- that is their size can easily be changed with .append()when an array is created: its size is set and cannot be changed easily
in multi-dimension lists, each "row" does not need to contain the same number of elements, each "column" does not require to contain the same number of elements.in multi-dimension arrays, each "row" MUST have the same number of elements, each "colomn" must have the same number of elements
multiple data types can be contained within the same listevery data type in the array must be the same type. 

It is the programmer's responsibility when  implementing an array structure in Python, to follow the restrictions that arrays have in other programming languages.

In mathematics, an array is called a matrix.

1D Array Examples

1D Example #1

#create an empty list
my_List = []

#print the list
print (my_List)

# create a new element in the list
my_List.append("Hello")

#print the list to see the changes
print (my_List)

# add more elements to the list
my_List.append("how")
my_List.append("are")
my_List.append("you")

#print the list to see the changes
print (my_List)

# change the value of each element
my_List[0]="Good-bye"
my_List[1]="see"
my_List[2]="you"
my_List[3]="later"

#print the list to see the changes
print (my_List)

# other list functions
print (len(my_List))

1D Example#2: 

    #create an list and fill it with zeros
    # Method #1
    wordlength = []
for r in range(10):
wordlength.append(0)
print (wordlength)

#create an list and fill it with zeros
    # Method #2
    wordlength=[0 for x in range(11)]
print(wordlength)

""" count the number of each word with length n """ #create an list and fill it with zeros wordlength=[0 for x in range(11)] # a list of words is required for this example, so make one my_List = [] my_List.append("Good-bye") my_List.append("see") my_List.append("you") my_List.append("later") # for each word in the list, increase the corresponding wordlenth element by one for word in my_List: wordlength[len(word)]+= 1 # neatly print the results print( "length "+"Number Of Words") # why does the range start a 1 and not 0 for i in range(1,len(wordlength)): print (" "*3+str(i)+" "*17+str(wordlength[i]) )

2D Array Examples: 

2D Example #1

# create an empty  list (array)
u=[]
print("the empty list")
print (u)
#fill the list (array) with no value (None)
for row in range(10):
# create a new empty row
u.append([])
for col in range (5):
#add a new element to the row
u[row].append(None)

# prints the entire list on one line
print("the 2D list")
print (u)

# place a value in a specific element
# values need not be the same type
u[3][2]="hello"
u[0][1]= 7

# prints each row on a line
print("the 2D list: one row at a time")
for row in range(10):
print(u[row])

# prints each element on a line
print("the 2D list: one element at a time")
for row in range(10):
for col in range(5):
print(u[row][col])

# print a specific element
print (u[0][1])

2D Example #2

#create a 2D list (array) and fill it with zeros
table= [ [ 0 for col in range(3) ] for row in range(6) ]

# print the 2D list on one line
print (table)

# add values to each element in the 2D list
for row in range(6):
for col in range(3):
table[row][col]= row*10+col

# print each row on one line
for row in range (6):
print (table[row])

# print the number of rows
print (len(table))

#print the number of cols in row #1
print(len(table[1]))
Comments