Gallery
Solution Album Upload
Share
Explore

icon picker
Array info

Array info

Python, arrays and lists
Python offers the list data structure rather than a classic array. There are important differences. The elements in a list can be mixed type and so a python array does not have a type although the values of the elements will have a type. Arrays tend to be fixed size whereas lists can increase and decrease the number of elements. These materials treat lists as arrays.
When you use an array in Python you need to:
give it a name and either:
state the size or dimension / length, ie number of elements or
assign a set of values to the array and this creates elements
In some situations the program developer will not know in advance how big the array needs to be. The program will need to ask the user how many items need to be stored and then set up the array.
Let’s see how this might be done.
There are 3 situations described below,
the first one is what you should be familiar with
the third one is what we need for the album upload program
the second situation is an in-between one that helps with understanding how it all works.

1. When you already know the list of values

Let’s start here. This is what you may know already about using an array in Python.
Assign values to array elements at the same time the array is set up.
the array size (number of elements) is automatically set to the number of values.
There are 3 text values so there will be 3 elements giving a size / length of 3.
# sets up an array of strings with list of values
nameOfArray = ['text A', 'text B', 'text C']

2. When you don’t have values but do know the array size

Sometimes the developer does know the size but does not have the values yet.
Set up an array and set the size (number of elements) without assigning values to the elements
Store the values into the elements later.
// set up array and set the size for 5 foods
fiveFavouriteFoods = [None] * 5

# store first value from keyboard
fiveFavouriteFoods[0] = input('Enter first food: ')

# store next value from keyboard
fiveFavouriteFoods[1] = input('Enter second food: ')

# duplicate for all five values
the None special value creates an empty array element.
The *5 sets up 5 empty elements.
So now fiveFavouriteFoods is an array with 5 empty elements ready to be used.

3. When you do not know the array size or the set of values

The program will get a size from the user first and then set up the array with that size.
ask the user how big the list is before declaring the array
declare the array and use the size that the user input
use a loop to input all the vlaues
# find out how many items in the list to be stored
numberOfItems = int(input('Enter number of values you want to store: '))

# set up array and set the size to match the number of items
itemList = [None] * numberOfItems

# loop and store list of values from keyboard
for i in range (0, len(itemList), 1) :
itemList[i] = input('Enter next value: ')
# end loop
Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.