Organizing Data in Main Memory
Basics
Data is often in the form of a sequence, a collection of things that are in a particular order. In Python, data can be in the form of a string, a sequence of characters. Data can also be in the form of a list, which is a sequence of data objects. Each element in a list can be of any type. The syntax for creating a list is quite simple. You can create an empty list or a list with several elements in it.
L = [] #Empty list
M = list() #Empty list
L = [2,4,6,8]
L = ['a','b','c','d']
L = ['C','S',1,2,6]
Notice the difference between defining a list and a string. A string is defined using quotations. A string can only contain a sequence of characters, which are also strings, individually. Lists are elements of any type. As you can see in the example above, a list can be empty, it can only contain numbers, it can only contain strings, it can have a combination of strings and integers, or it can include any combination of data objects.
One practical application of using strings is in the example below. Here, we have a program that takes a list of numbers from the user, calculates their statistical average, and then prints the average on the screen. In this program, we will learn several new Python tools.
S = input()
S = S.split(' ')
L = list(map(eval, S))
avg = round(sum(L)/len(L), 2)
print(avg)
First, we are using the input
function. The user is supposed to enter the numbers one after the other, separated by spaces. For example, the user enters 2 4 5 6 8 10 11 19
. This value is stored as a string in S
since the input
function always returns a string. Next, we will use a split
to split a string into a list of elements. To tell the split
method how to split the string, we will specify that a space separates each element. Thus, we write S.split(' ')
, which specifies the space as a separator argument for split
. The result is now a list, which we will store in S
. The list looks like: ['2', '4', '5', '6', '8', '10', '11', '19']
.
But observe something special about the list above. Its elements are all strings. That means we cannot perform arithmetic operations on them to calculate their average. We will have to convert the type for each element from string to a numeric type. Luckily, there is a built-in function that can help us achieve this. The map(f,v)
function takes two arguments. One is a function name f,
and one is a sequence name v
. The map
function applies the given function f
to each element in the sequence v
. In our example, we apply the function eval
to elements of S
. eval
is a more general form of int
and float
. If an element in v
is a string with only an integer, eval
will convert its type to integer. If otherwise, the element has a decimal point, its type will be converted to float. Finally, map
returns a map object, which we do not want to deal with now. Thus, we will apply the list
function to the result of map
. In the end, the variable L
will have the value [2, 4, 5, 6, 8, 10, 11, 19]
.
We are almost done. We have a list of numbers L
for which we want to calculate the average. All we need is a function that gives us the sum of all values in L
. This function is called sum
. Thus, sum(L)/len(L)
will give us what we need. We can finally print the results.
Operations on lists
We can use the concatenation and the repetition operators for lists, similar to strings. One can put two or more lists together using the +
operator, and one can produce a new list by repeating the items of an existing list using the *
operator.
L = [1,2] + [3,4] #This produces [1,2,3,4]
L = [0] * 4 #This produces [0,0,0,0]
We can also add or delete items to or from a list. The order of items in a list can be reversed. We can search for an item in a list. We can count the occurrences of an item in a list. Finally, we can find the maximum and the minimum value in a list. Without using the Python compiler, try to produce the values displayed by the following program to check the understanding of lists and their operations.
L = [1,2]
L.append(3) #Add element 3 to the end of L.
L.extend([1,2,4,5]) #Add the list argument to the end of L.
L.reverse() #Reverse the order of elements in L.
L.remove(4) #Remove the element 4 from L.
del L[0] #Delete the first element in L.
print(L)
print(max(L), min(L), L.count(1), L.index(2))
List Slicing
A list slice is similar to a string slice. If we had to choose some of the elements in a list and create a new one, we could accomplish that using slicing. A slice specifies two arguments, from and to, which select the range of indexes from which the slice should be created.
For example, if we want to slice elements of L
from index 2 to index 5, including index 5, we have to write: L[2:6]
.
Slicing can have a third optional argument, k
as in L[2:10:3]
. This code specifies elements from index 2 to index 9, every three elements. That is, the element on index 2 is included, and then the next element will be 5, the next will be 8, and that is it.
Tuples (immutable sequences)
Similar to lists, a tuple is a sequence of any objects. Tuples are different from lists in that they are immutable. That is, once we define a tuple, we cannot add to it or remove anything from it.
t = tuple() #Empty tuple
t = (1,2,3,4)
print(len(t))
print(sum(t))
s = 5,6,7,8
print(t+s)
print(t.index(2))
A tuple can be defined using parentheses, separating each object with a comma. However, parentheses are optional, and we can just use commas.
Standard concatenation and repetition operators do work with tuples, similar to lists and strings. However, methods such as append
or remove
are not defined for tuples.
The operator library
There are cases where we need to use arithmetic operators as functions. We can do this by using the operator library. Suppose we want to add the elements of two lists L and M, using the map function. Adding two lists is possible using the addition operator imported from the operator library.
from operator import add
L = list(map(eval,input().split()))
M = list(map(eval,input().split()))
N = list(map(add,L,M))
In the program above, N
contains the element-wise addition of two lists, L
and M
. Note that the map function receives three arguments here: the function, the first list, and the second list. The first list is used as the function's first argument, and the second list is used as the second argument of the function.