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:

['-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
tripleee
  • 175,061
  • 34
  • 275
  • 318
celibon
  • 17
  • 3

3 Answers3

1

The following code should produce a nested list as expected:

Code:

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

Output:

[['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'], ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-']]
ScottC
  • 3,941
  • 1
  • 6
  • 20
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', '-', '-', '-', '-', '-', '-', '-', '-', '-']]

Yash Mehta
  • 2,025
  • 3
  • 9
  • 20
0

You can use python itertools.repeat()

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

enter image description here

hyunjun
  • 21
  • 5
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Dec 24 '22 at 10:17