[[+content_image]]
P
P
Pavel Kityan2015-10-22 01:50:42
Python
Pavel Kityan, 2015-10-22 01:50:42

Why are the elements connected?

Childish question for pythonists.
When creating a two-dimensional list like this:

matrix = [[0] * 10] *10;
matrix[1][1] = 100;
print matrix;

I get the value 100 in each nested list.
Does this method work by reference?
Thank you.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
V
Vitaly, 2015-10-22
@bookworm

Yes, you could even say, in a sense, that everything in Python is passed by reference. But data types are divided into mutable and immutable. Lists are mutable, but numbers are not.
Arrays can be specified using a special syntax - list comprehensions:
matrix = [[0]*10 for x in range(10)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question