Home > Backend Development > Python Tutorial > Why Does Modifying One Sublist Affect All Sublists When Using List Multiplication in Python?

Why Does Modifying One Sublist Affect All Sublists When Using List Multiplication in Python?

DDD
Release: 2024-12-22 08:24:10
Original
1081 people have browsed it

Why Does Modifying One Sublist Affect All Sublists When Using List Multiplication in Python?

List of Lists Changes Reflected Unexpectedly Across Sublists

In Python, creating a list of lists using list multiplication, as in [[1] 4] 3, results in a situation where changes to one sublist affect all sublists. This behavior is counterintuitive and can lead to unexpected consequences.

Explanation

When using list multiplication, the new list is a reference to the original list. When you modify an element of the new list, you are actually modifying the element in the original list that is referenced by all sublists.

This unexpected behavior occurs because Python evaluates the multiplication operator before the list comprehension. In this case, [[1] 4] evaluates to a single sublist, and * simply creates three references to that sublist.

Solution

To avoid this behavior and create three independent sublists, you can use a list comprehension, as follows:

[[1] * 4 for _ in range(3)]
Copy after login

The list comprehension re-evaluates the [1] * 4 expression for each sublist, resulting in three distinct sublists that are not linked.

Comparison with List Multiplication

While list multiplication is a convenient way to create lists with repeating elements, it is important to understand its limitations. List multiplication operates on existing objects without seeing expressions. It does not make copies of objects but instead creates references to existing objects.

In contrast, list comprehensions re-evaluate expressions for each element, creating new objects as needed. This behavior ensures that each element in the list is independent.

Additional Resources

  • [How do I clone a list so that it doesn't change unexpectedly after assignment?](https://stackoverflow.com/questions/19850422/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment)
  • [How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python?](https://stackoverflow.com/questions/6951326/how-to-initialize-a-two-dimensional-array-list-of-lists-if-not-using-numpy-in-pyt)
  • [List of dictionary stores only last appended value in every iteration](https://stackoverflow.com/questions/10827325/list-of-dictionary-stores-only-last-appended-value-in-every-iteration)

The above is the detailed content of Why Does Modifying One Sublist Affect All Sublists When Using List Multiplication in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template