Your code is well written, but it will break if certain conditions are met. The intended input appears to be something like '2 3 1 2 3 4 5 6', where the first two are the dimensions, and the rest is the the content of the array. Now, if you were to input '5 5 2', the script expects 25 values of content, but it only received 1 value, '2'.
The error you show is because you are attempting to use list comprehension syntax outside of a list. For example:
print([i for i in range(10)])
#OUTPUT: 0 1 2 3 4 5 6 7 8 9
print(i for in range(10))
#OUTPUT: SyntaxError: invalid syntax
The above example shows a simplified example of your error.
Below is a version of your code which has descriptive comments to describe what the list comprehension is doing when it is called.
# We need to know how big to make the array, Let's do this below with inputs.
print('Enter m:')
m = int(input())
print('Enter n:')
n = int(input())
print('m: ',m,' n: ',n)
#Let's now get the values for the array. Let's do this with inputs again.
print('Enter ',m*n,' values below, delimited by spaces. (ex. `1 2 3 4 5 ...`):')
nums = input().split()
nums = [int(i) for i in nums] #Using list comprehension to change strs to ints
#Now let's assign those vals to an array
ar = [nums[i*m:i*m+m] for i in range(n)] #using List comprension to distribute nums into an array
print('Your Array: ',ar)
#Now lets print it cleanly.
print('Your Array, Printed Cleanly:')
for i in ar:
row = ""
for j in i:
row = row+str(j)+' '
print(row)
Running this code, and entering descriptive values gives:
Enter m:
> 2
Enter n:
> 5
m: 2 n: 5
Enter 10 values below, delimited by spaces. (ex. `1 2 3 4 5 ...`):
> 0 1 2 3 4 5 6 7 8 9
Your Array: [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]]
Your Array, Printed Cleanly:
0 1
2 3
4 5
6 7
8 9