Initializing a 2d array in one line in Python
Question:
I am wondering, how i can shorten this:
test = [1, 2, 3]
test[0] = [1, 2, 3]
test[1] = [1, 2, 3]
test[2] = [1, 2, 3]
I tried something like this:
test = [1[1, 2, 3], 2 [1, 2, 3], 3[1, 2, 3]]
#or
test = [1 = [1, 2, 3], 2 = [1, 2, 3], 3 = [1, 2, 3]] #I know this is dumb, but at least I tried...
But it’s not functioning 😐
Is this just me beeing stupid and trying something that can not work, or is there a proper Syntax for this, that I don’t know about?
Answers:
It’s a list comprehension:
test = [[1, 2, 3] for i in range(3)]
The simplest way is
test = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
But, if you want to have more number of lists to be created then you might want to go with list comprehension, like this
test = [[1, 2, 3] for i in range(100)]
This will create a list of 100 sub lists. The list comprehension is to create a new list and it can be understood like this
test = []
for i in range(100):
test.append([1, 2, 3])
Note: Instead of doing test[0] = ...
, you can simply make use of list.append
like this
test = []
test.append([1, 2, 3])
...
If you look at the language definition of list,
list_display ::= "[" [expression_list | list_comprehension] "]"
So, a list can be constructed with list comprehension or expression list. If we see the expression list,
expression_list ::= expression ( "," expression )* [","]
It is just a comma separated one or more expressions.
In your case,
1[1, 2, 3], 2[1, 2, 3] ...
are not valid expressions, since 1[1, 2, 3]
has no meaning in Python. Also,
1 = [1, 2, 3]
means you are assigning [1, 2, 3]
to 1
, which is also not a valid expression. That is why your attempts didn’t work.
Your code: test = [1 = [1, 2, 3], 2 = [1, 2, 3], 3 = [1, 2, 3]]
is pretty close. You can use a dictionary to do exactly that:
test = {1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]}
Now, to call test 1 simply use:
test[1]
Alternatively, you can use a dict comprehension:
test = {i: [1, 2, 3] for i in range(3)}
If you want, you can do this:
test = [[1,2,3]]*3
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
===== Edited ====.
However, Note that all elements refer to the same object
# 1. -----------------
# If one element is changed, the others will be changed as well.
test = [[1,2,3]]*3
print(test)
#=>[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
test[0][1] = 4
print(test)
#=>[[1, 4, 3], [1, 4, 3], [1, 4, 3]] # because it refer to same object
# 2. -----------------
# Of course, those elements have the same value.
print("same value") if test[0] == test[1] else print("not same value")
#=> same value
# 3. -----------------
# Make sure that All refer to the same object
print("refer to the same object") if test[0] is test[1] else print("not refer to the same object")
#=> refer to the same object
# 4. -----------------
# So, Make sure that All have same id
hex(id(test[0]))
#=>e.g. 0x7f116d337820
hex(id(test[1]))
#=>e.g. 0x7f116d337820
hex(id(test[2]))
#=>e.g. 0x7f116d337820
I am wondering, how i can shorten this:
test = [1, 2, 3]
test[0] = [1, 2, 3]
test[1] = [1, 2, 3]
test[2] = [1, 2, 3]
I tried something like this:
test = [1[1, 2, 3], 2 [1, 2, 3], 3[1, 2, 3]]
#or
test = [1 = [1, 2, 3], 2 = [1, 2, 3], 3 = [1, 2, 3]] #I know this is dumb, but at least I tried...
But it’s not functioning 😐
Is this just me beeing stupid and trying something that can not work, or is there a proper Syntax for this, that I don’t know about?
It’s a list comprehension:
test = [[1, 2, 3] for i in range(3)]
The simplest way is
test = [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
But, if you want to have more number of lists to be created then you might want to go with list comprehension, like this
test = [[1, 2, 3] for i in range(100)]
This will create a list of 100 sub lists. The list comprehension is to create a new list and it can be understood like this
test = []
for i in range(100):
test.append([1, 2, 3])
Note: Instead of doing test[0] = ...
, you can simply make use of list.append
like this
test = []
test.append([1, 2, 3])
...
If you look at the language definition of list,
list_display ::= "[" [expression_list | list_comprehension] "]"
So, a list can be constructed with list comprehension or expression list. If we see the expression list,
expression_list ::= expression ( "," expression )* [","]
It is just a comma separated one or more expressions.
In your case,
1[1, 2, 3], 2[1, 2, 3] ...
are not valid expressions, since 1[1, 2, 3]
has no meaning in Python. Also,
1 = [1, 2, 3]
means you are assigning [1, 2, 3]
to 1
, which is also not a valid expression. That is why your attempts didn’t work.
Your code: test = [1 = [1, 2, 3], 2 = [1, 2, 3], 3 = [1, 2, 3]]
is pretty close. You can use a dictionary to do exactly that:
test = {1: [1, 2, 3], 2: [1, 2, 3], 3: [1, 2, 3]}
Now, to call test 1 simply use:
test[1]
Alternatively, you can use a dict comprehension:
test = {i: [1, 2, 3] for i in range(3)}
If you want, you can do this:
test = [[1,2,3]]*3
#=> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
===== Edited ====.
However, Note that all elements refer to the same object
# 1. -----------------
# If one element is changed, the others will be changed as well.
test = [[1,2,3]]*3
print(test)
#=>[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
test[0][1] = 4
print(test)
#=>[[1, 4, 3], [1, 4, 3], [1, 4, 3]] # because it refer to same object
# 2. -----------------
# Of course, those elements have the same value.
print("same value") if test[0] == test[1] else print("not same value")
#=> same value
# 3. -----------------
# Make sure that All refer to the same object
print("refer to the same object") if test[0] is test[1] else print("not refer to the same object")
#=> refer to the same object
# 4. -----------------
# So, Make sure that All have same id
hex(id(test[0]))
#=>e.g. 0x7f116d337820
hex(id(test[1]))
#=>e.g. 0x7f116d337820
hex(id(test[2]))
#=>e.g. 0x7f116d337820