sec02 - list Copying(2) [type() and id()]

In this lecture, we will briefly review what we learned in the previous "[Column] How to Manage Variables and Data in Python," and learn how to use the type() and id() functions.

スポンサーリンク

Checking type() and id() for Numbers and Strings

Checking type() and id() for Numbers

Let's check the type and id of numeric data to see how Python manages data.

Use the type() function to check the data type and the id() function to check the id.

num1 = 5
num2 = num1
num3 = 5
num4 = 5.0

print('num1:', num1, type(num1), id(num1))  # 5  140715724784680
print('num2:', num2, type(num2), id(num2))  # 5  140715724784680
print('num3:', num3, type(num3), id(num3))  # 5  140715724784680
print('num4:', num4, type(num4), id(num4))  # 5.0  1872433047024

When you run the above code, you will get output similar to the figure below. (Note that the value returned by id() changes each time you run it.)

It can be confirmed that all variables assigned the value of int5 refer to the same ID number, while int5 and float5.0 are generated as separate data with different id numbers.

Checking type() and id() for Strings

Data management for strings is similar to that for numbers, but the approach may vary depending on how the string is generated, potentially leading to its creation as a separate object.

str1 = 'Hello'
str2 = 'Hello'
str3 = '{}llo'.format('He')
str4 = 'Hello World'[:5]

print('str1:', str1, type(str1), id(str1))  # Hello  2321373022176
print('str2:', str2, type(str2), id(str2))  # Hello  2321373022176
print('str3:', str3, type(str3), id(str3))  # Hello  2321372756752
print('str4:', str4, type(str4), id(str4))  # Hello  2321373022656

When the above code is executed, the variables "str1" and "str2" will have the same id number, but "str3" and "str4" will have different id numbers. Although all variables contain the string 'Hello', using format() or slicing creates separate objects due to differences in how the string is generated.

Checking type() and id() of a list

Next, let's check the information of a list and its elements. (In the code below, "original_list" is abbreviated as "original".)

original = [1, 2, 3]

print('original:', original, type(original), id(original))  # [1, 2, 3] <class 'list'> 2206846990848
print('original[0]:', original[0], type(original[0]), id(original[0]))  # 1 <class 'int'> 140715703092136
print('original[1]:', original[1], type(original[1]), id(original[1]))  # 2 <class 'int'> 140715703092168
print('original[2]:', original[2], type(original[2]), id(original[2]))  # 3 <class 'int'> 140715703092200

When you run this code, you get the information of the list and the int elements, as described in the comments above.

Running print(original) outputs [1, 2, 3], which may give the impression that the numbers are contained directly. However, the list does not store the int values themselves; it references the data of each value. Using type() and id() to check this information helps deepen your understanding of Python's data management.

(※ During development, it's common and acceptable to check list elements with outputs like [1, 2, 3]. However, when copying structures like lists, problems can occur, so it's important to know how to check their type and id information.)

スポンサーリンク

Checking type() and id() of copied lists

Example referencing the same list object

Let's check the id number of a list copied with copied = original.

original = [1, 2, 3]
copied = original

print('original:', original, type(original), id(original))  # [1, 2, 3] <class 'list'> 1896325197824
print('copied:', copied, type(copied), id(copied))          # [1, 2, 3] <class 'list'> 1896325197824

Running this code confirms that both lists have the same id number.

Example referencing different list objects

By generating list data as shown below, each variable can reference a different list.

original = [1, 2, 3]
copied = [1, 2, 3]
print('original:', original, type(original), id(original))  # [1, 2, 3] <class 'list'> 2916726571520
print('copied:', copied, type(copied), id(copied))          # [1, 2, 3] <class 'list'> 2916726483584
original = [1, 2, 3]
copied = original[:]
print('original:', original, type(original), id(original))  # [1, 2, 3] <class 'list'> 1795666379264
print('copied:', copied, type(copied), id(copied))          # [1, 2, 3] <class 'list'> 1795666291328
original = [1, 2, 3]
copied = original.copy()
print('original:', original, type(original), id(original))  # [1, 2, 3] <class 'list'> 2438443153920
print('copied:', copied, type(copied), id(copied))          # [1, 2, 3] <class 'list'> 2438443176704

Checking type() and id() of nested lists (Shallow copy)

Let's check the id numbers of the "outer" and "inner" lists in a nested list.

original = [[1, 2]]
copied = original.copy()
copied[0][0] = 99

print('original:', original, type(original), id(original))  # [[99, 2]] <class 'list'> 3051058725056
print('copied:', copied, type(copied), id(copied))          # [[99, 2]] <class 'list'> 3051058637248
print('original[0]:', original[0], type(original[0]), id(original[0]))  # [99, 2] <class 'list'> 3051058739776
print('copied[0]:', copied[0], type(copied[0]), id(copied[0]))          # [99, 2] <class 'list'> 3051058739776

The "outer" list has a different id because it was copied using the copy() method. However, the "inner" list shares the same id. Therefore, any changes made to the elements of the "inner" list affect both lists.

In other words, with a Shallow copy, you cannot treat all nested lists as completely independent. (※However, if you do not intend to modify the elements of the nested lists, a Shallow copy is sufficient.)

Checking type() and id() after Deep copy

Next, let's check the type() and id() after performing a Deep copy. In Python, we use the deepcopy() function from the copy module.

import copy

original = [[1, 2]]
copied = copy.deepcopy(original)
copied[0][0] = 99

print('original:', original, type(original), id(original))  # [[1, 2]] <class 'list'> 1461724428864
print('copied:', copied, type(copied), id(copied))          # [[99, 2]] <class 'list'> 1461724451776
print('original[0]:', original[0], type(original[0]), id(original[0]))  # [1, 2] <class 'list'> 1461724327744
print('copied[0]:', copied[0], type(copied[0]), id(copied[0]))          # [99, 2] <class 'list'> 1461724332416

You can see that all id numbers, including those of the nested lists, are different. This ensures that modifying one nested list does not affect the other.

(※ However, creating full copies can affect memory usage and performance, so choose between Shallow copy and Deep copy depending on the task.)

スポンサーリンク