0

I want to create a nested list in one line. List will be:

a = [["-","-",..."-"],["-","-",..."-"],...["-","-",..."-"]]

Each inner list should have 10 "-"s and the nested list should have 10 lists in it.

I've tried that one but it does not work as i wanted:

a = ["-","-","-","-","-","-","-","-","-","-"]*10

print(a)

Output was:

['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
0

3 Answers 3

1

The following code should produce a nested list as expected:

Code:

a = [["-"]*10]*10
print(a)

Output:

[['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']]
Sign up to request clarification or add additional context in comments.

Comments

0
#Not in refrenced form
a = [['-' for col in range(10)] for row in range(10)] 
print(a)

#If you wanted in refrenced form
a=[['-']*10]*10
print(a)

Example of not refrenced..

a = [['-' for col in range(10)] for row in range(10)] 
a[0][0]='change'
print(a)

Output:-

[['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']]

Example of referenced..

a=[['-']*10]*10
a[0][0]='change'
print(a)

Output:-

[['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['change', '-', '-', '-', '-', '-', '-', '-', '-', '-']]

Comments

0

You can use python itertools.repeat()

from itertools import repeat
list(repeat(["-"]*10, 10))

enter image description here

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.