Sunday, July 24, 2022

Are lists mutable?

Unlike strings, lists are mutable because you can change the order of items in a list or reassign an item in a list. To access the elements of a list you have to use the same syntax as in the case of accessing the elements of a string i.e. the name of a variable that contains a specific string/list followed by the bracket operator. The general syntax of accessing the list element can be written as:
list_name[i]
where the list_name is the variable under which the list is saved and i is an integer number that represents the position of the list element i.e. index. The range of list elements is from 0 to len(list_name)-1. You have to remember that the indices start from 0.
Example 1Create list with numbers from 0 to 10 and write the program that will write all list elements in form "list_name[i] = list_name[i]".
SolutionIn the Example description the print syntax may be a little confusing but soon it will be explained. Ok, so we start the program by generating the list in one code line.
list11 = [i for i in range(0,11,1)]
We have generated a list with 11 elements in the range from 0 to 10 under a variable named list11. To print out each element in the list we will use the for loop and in the for loop, we will create the print function that will print out each element of the list.
for i in range(len(list11)):
print("list11[{}] = {}".format(i, list11[i])
In each iteration, the print function will print each element of the list. The format function will insert the integer in the first set of curly brackets, and insert the list value in the second set of curly brackets. The generated output is given below.
list1[0] = 0
list1[1] = 1
list1[2] = 2
list1[3] = 3
list1[4] = 4
list1[5] = 5
list1[6] = 6
list1[7] = 7
list1[8] = 8
list1[9] = 9
list1[10] = 10
When compared to strings, lists are mutable because we can change the order of items in a list and reassign an item in a list. The general form of assigning a new value to an element can be written as:
list_name[index] = new_value
Example 2 Using previous list from Example 1 assign values (300, 500, 700, 900) to elements with indices 2, 4, 6, and 8. Then print out all list elements.
Solution: We have to manually assing values to elements at specific indices.
list11[2] = 300
list11[4] = 500
list11[6] = 700
list11[8] = 900
To print out each element from the list use the code from Example 1.
for i in range(len(list11)):
print("list11[{}] = {}".format(i, list11[i]))
This will generate the following output.
list1[0] = 0
list1[1] = 1
list1[2] = 300
list1[3] = 3
list1[4] = 500
list1[5] = 5
list1[6] = 700
list1[7] = 7
list1[8] = 900
list1[9] = 9
list1[10] = 10
The list is a relationship between indices and elements and this relationship is called mapping i.e. each index "maps to" one of the elements. The list indices work in the same way as string indices which means that any integer expression can be used as an index. Then "IndexError" will be generated for reading and writing an element that does not exist. If the index is a negative value then it counts backward from the end of the list.
Example 3 Create a list that contains 6 elements from 0 to 5 (including 5) and print out the list elements from the last to the first. Finally, cause the IndexError.
Solution:Generating a list with previous description is pretty simple.
list22 = [i for i in range(0,6,1)]
To print out each element of the list backward type in the following code.
for i in range(len(list22)):
print("list22[{}] = {}".format(len(list22)-1-i,list22[len(list22)-1-i]))
We know that the previous code is not very elegant however, we will explain it in detail. The most concerning part of the previous code is a print function i.e. the argument of the function. We will start from the string "list22[{}] = {}" which is a string that will be printed out in each iteration of a for loop. The two curly brackets are elements in the string that will be filled with two format function elements. The first element is a list index which is in this case defined as len(list22)-1-i. If len(list22) is a built-in "len" function that gives us an integer number of how many elements are in the list. In this case, we have 6 elements from 0 to 5. If we typed the list22[len(list22)] this would generate the IndexError since the indices of elements in a list is from 0 to 5 and we have given a command a Python to print out an element with index 6 which does not exist. The first format element consists of len(list22) which is always equal to 6, -1, and i which is the iteration number of for loop. If we have typed len(list22)-i the sequence of the first format element to all iterations in for loop would be:
len(list22)-i = 6 - 0 = 6
len(list22)-i = 6 - 1 = 5
len(list22)-i = 6 - 2 = 4
len(list22)-i = 6 - 3 = 3
len(list22)-i = 6 - 4 = 2
len(list22)-i = 6 - 5 = 1
As seen from previous values the indices are wrong since the last index is 5, not 6, and the first index is 0, not 1. To solve this problem we have introduced -1.
len(list22)-1-i = 6 - 1 = 5
len(list22)-1-i = 6 - 2 = 4
len(list22)-1-i = 6 - 3 = 3
len(list22)-1-i = 6 - 4 = 2
len(list22)-1-i = 6 - 5 = 1
len(list22)-1-i = 6 - 6 = 0
The second element of the format function is list22[len(list22)-1-i]. This element will retrieve each element of a list. If -1 is not in the index equation (len(list22) -1 -i) it would generate the IndexError since it would start from 6 not from index 5. The output of the for loop is given below.
list22[5] = 5
list22[4] = 4
list22[3] = 3
list22[2] = 2
list22[1] = 1
list22[0] = 0

No comments:

Post a Comment