sec02 - list Editing
スポンサーリンク

Overwriting list elements

Specify one index in the list and overwrite the data

When overwriting list elements, specify the index as in var[n] = data and write the data you want to overwrite.

Let's execute the following code to confirm that the data at the specified index is overwritten.

sample_list = ['a', 'b', 'c', 'd', 'e']
sample_list[0] = 1
print(sample_list)  # [1, 'b', 'c', 'd', 'e']
print(len(sample_list))  # 5

The above code overwrites the element at index [0] with 1. The number of elements is 5, with no increase or decrease from the original data.

Next, overwrite the elements of index [0] with [1, 2, 3].

sample_list = ['a', 'b', 'c', 'd', 'e']
sample_list[0] = [1, 2, 3]
print(sample_list)  # [[1, 2, 3], 'b', 'c', 'd', 'e']
print(len(sample_list))  # 5

The result of the above code is [[1, 2, 3], 'b', 'c', 'd', 'e']. In this case, the list [1, 2, 3] remains unchanged and is overwritten at index [0]. The number of elements is 5, with no increase or decrease from the original data.

When overwriting by specifying a single index, the replaced data remains unchanged and is replaced at the position of that index. This behavior is easy to understand.

Specify the range of data to be overwritten using a list slice

When specifying a list range using a slice, write it as var[n:m] = iterable_data. Note that the data to be assigned must be iterable.

What is iterable data?

Iterable data types will be explained in more detail in a separate lecture, but here is a brief overview.

  • Data types that can hold multiple values, such as lists
    • list, tuple, dict, set, string
      ※ Since strings also hold multiple pieces of character information, they are considered iterable data types.
      ※ Numeric values (int, float) are not iterable data types.
  • Objects that can generate data sequentially
    • These include "range" objects returned by the range() function, which you will learn about in the data structure section.
      ※ "range" objects generate numbers in the specified range in sequence and return the values.

The key point to remember from this lecture is that when you want to overwrite data after specifying a slice range (var[n:m]), you cannot assign non-iterable data types such as numbers. (However, it is OK if the list contains numbers as elements, such as [1].)

Now, let's look at a specific example of specifying a range with slices and overwriting the data.

First, substitute [1] for the range [0:4] in sample_list. Let's execute the following code.

sample_list = ['a', 'b', 'c', 'd', 'e']
sample_list[0:4] = [1]
print(sample_list)  # [1, 'e']
print(len(sample_list))  # 2

The result is [1, ‘e’]. The elements in the range [0:4] have been completely replaced. The assigned value is not [1], but rather the elements of the list are expanded and incorporated into the elements of “sample_list.” In other words, the list ([1]) is not replaced as-is, but rather the elements of the list are expanded, and the expanded 1 is replaced within the range [0:4].

Next, let's replace it with a list that has multiple elements.

sample_list = ['a', 'b', 'c', 'd', 'e']
sample_list[0:1] = [1, 2, 3]
print(sample_list)  # [1, 2, 3, 'b', 'c', 'd', 'e']
print(len(sample_list))  # 7

The result is [1, 2, 3, 'b', 'c', 'd', 'e']. The range [0:1] is the same as [0], but when specifying a range using a slice, the values of the iterable data type are always expanded and replaced.

The difference between specifying a single index and specifying a range with a slice is as follows.

  • sample_list[0] = [1, 2, 3]
    • # [[1, 2, 3], 'b', 'c', 'd', 'e']
  • sample_list[0:1] = [1, 2, 3]
    • # [1, 2, 3, 'b', 'c', 'd', 'e']

+ Operator

Lists can be combined using the + operator. As with strings, operations between different data types are not possible. When using the + operator, the data types on both sides must be lists.

Let's run the following code and check the results.

sample_list = ['a', 'b', 'c']
new_sample_list = sample_list + [1, 2, 3]
print(new_sample_list)  # ['a', 'b', 'c', 1, 2, 3]
print(sample_list)  # ['a', 'b', 'c'] (The original list remains unchanged.)

The above code combines ['a', 'b', 'c'] assigned to the variable ‘sample_list’ and the list [1, 2, 3] using the + operator to generate a new list. The generated list is assigned to the variable “new_sample_list.”

The newly created list (['a', 'b', 'c', 1, 2, 3]) contains the elements of the original two lists in order.
Note that the contents of the variable “sample_list” remain unchanged.

スポンサーリンク

* Operator

Using the * operator allows you to generate a new list in which the elements of the original list are repeated a specified number of times. This behaves similarly to repeating a string.

Let's execute the following code to confirm the behavior.

sample_list = ['a', 'b', 'c']
new_sample_list = sample_list * 4
print(new_sample_list)  # ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
print(sample_list)  # ['a', 'b', 'c'] (The original list remains unchanged.)

A new list containing the elements ['a', 'b', 'c'] repeated four times is generated and assigned to the variable “new_sample_list”.

スポンサーリンク