0

I am new to programming and starting to learn Python Programming (Purely for the Buzz). So while learning 2d list and list comprehension. I am getting an error as 'Invalid Syntax' while trying to print a 2d list using list comprehension. Honestly speaking a little bit confused about list comprehension. A detailed explanation of list comprehension would be great.

st=input().split()
n,m=int(st[0]),int(st[1])
st=st[2:]
li=[[int(st[m*i + j]) for j in range(m)] for i in range(n)]
print(' '.join([str(ele) for ele in row])) for row in li

enter image description here

  • Your code seems correct. The error you're experiencing is not in the snippet of the code you've shared here. If you could share the complete error message, it would help find the issue. – elmiomar Jun 13 '23 at 15:36
  • sure i will share it with you. – Km Shrikanth Jun 13 '23 at 15:38
  • The error is in the shared code - it's that you can't have a comprehension without braces (or parens). So the `print` line is failing. `[print(...) for row in li]` would work, though that's a bit of an abuse of comprehensions. – Nathaniel Ford Jun 13 '23 at 15:47
  • 1
    @KmShrikanth you're trying to execute a statement (`print`) for each item in a list (`li`). List comprehensions are used for creating new lists, not for executing statements. The code you had before with the for loop is the correct way to do it. – elmiomar Jun 13 '23 at 15:49
  • Okay understood. Thank you – Km Shrikanth Jun 13 '23 at 15:56

1 Answers1

1

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 
Jonathan F.
  • 215
  • 1
  • 11