6.3.1 – One-dimensional lists

6.3.1 – One-dimensional lists

Store related values in one-dimensional lists, read and replace items by index, and trace changes that shift positions. Use slices and indexed loops to copy and update a complete array.

Represent a sequence with a list

A structured data type groups items that themselves have types. An array holds items of the same type with a shared purpose, such as several lap times. Pearson's Python subset represents arrays with lists.

Python
lap_times = [68, 65, 71, 66]

The square brackets contain four integer items in a defined order. One list is easier to traverse than unrelated variables named time1, time2 and so on. Its length can describe how many values are currently stored. A one-dimensional structure needs one index to select an item.

Python lists can also represent records: different fields about one entity. Records and rectangular two-dimensional structures are developed in their own lesson. Here all items in an array have the same role and type. The concept comes from the data's meaning, not just from seeing square brackets.

To start with no items, both of these create an empty list:

Python
values = []
queue = list()

An empty list has length zero; it does not contain a blank item. list() can also convert a string into a list of its characters:

Python
letters = list("CAT")
print(letters)
Output
['C', 'A', 'T']

Read and replace list items

An index identifies an item's position. Python counts from zero. For a list of length n, non-negative valid indexes are 0 through n−1.

Python
routes = ["Amber", "Blue", "Green", "Red"]
print(routes[2])
routes[3] = "Silver"
print(routes)
Output
Green
['Amber', 'Blue', 'Green', 'Silver']

The indexed assignment replaces the final item without adding an item or changing length. Lists are mutable: an existing item can be changed. Index 4 is outside this four-item list and would raise IndexError. Reading index 0 of an empty list also fails because no item exists.

Python
values = [4, 7, 9]
if len(values) > 0:
    print(values[len(values) - 1])

The length check prevents an invalid final-item access for an empty list. The final non-negative index is length minus one. Negative indexing is valid Python too; -1 selects the last item of a nonempty sequence, but it does not make empty-list access safe.

Append, insert and delete

Use the operation that matches the required change. append adds at the end; insert adds before a position; del removes an item; indexed assignment replaces without changing length.

Python
queue = list()
queue.append("Ada")
queue.append("Bo")
queue.append("Cy")
queue.insert(1, "Eli")
queue[2] = "Bea"
del queue[3]
print(queue)
After actionqueue
Three appends['Ada', 'Bo', 'Cy']
Insert at 1['Ada', 'Eli', 'Bo', 'Cy']
Replace at 2['Ada', 'Eli', 'Bea', 'Cy']
Delete at 3['Ada', 'Eli', 'Bea']

Insertion shifts the old item at index 1 and later items right. Deletion shifts later items left. Therefore the same numeric index may refer to a different item after a length change. Re-read the current state instead of using an index from the original list.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account