0

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?

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)}

Comments

0

It's a list comprehension:

test = [[1, 2, 3] for i in range(3)]

Comments

0

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

1 Comment

don't do this unless you want all rows to be identical

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.