-1

I just new in python, I code for fetch array value from user, for this reason I asked a question yesterday in stackoverflow. Darius Morawiec and Austin give me the best salutation, but I don't understand the flow of for loop, I google it, but I don't understand those explanation.can any body explain the control of "for" loop for given code below. Thank you

arr = [[int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1))) 
       for c in range(n_cols)] 
       for r in range(n_rows)]

yesterday conversation Link

4
  • 1
    You could've just asked for this clarification on the answer you got the code from. Commented Feb 13, 2019 at 19:03
  • I already asked him through comment, but I need the explanation as soon as possible Commented Feb 13, 2019 at 19:08
  • If you search on the phrase "Python list comprehension", you’ll find resources that can explain it much better than we can in an answer here. Commented Feb 13, 2019 at 19:19
  • thank you @prune I must check it :) Commented Feb 13, 2019 at 19:31

2 Answers 2

0

A list comprehension is a compressed syntax in python to generate a list. If you rewrite this, it will probably be very clear.

The general syntax is: [expression for element in list (optional: if condition)] and it returns a list

This is exactly the same as writing:

new_list = []
for element in original_list:
    if (condition):   
        new_list.append(expression)

in your case, you can rewrite the two list comprehensions (they are nested) like so:

arr = [[second comprehension] for r in range(n_rows)] ->

arr = []
for r in range(n_rows):
    arr.append(second list)

now, for the second list comprehension:

second_list = []
for c in range(n_cols):
    entry = int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1)))
    second_list.append(entry)

And the entire flow is:

arr = []
for r in range(n_rows):
    second_list = []
    for c in range(n_cols):
            entry = int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1)))
            second_list.append(entry)
    arr.append(second list)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Tacratis i got my best salutation thanks a lot :)
0

Despite share the same keywords, that's not a for loop; it's a list comprehension nested in another list comprehension. As such, you need to evaluate the inner list first:

[
  [int(input("Enter value for {}. row and {}. column: ".format(r + 1, c + 1))) 
     for c in range(n_cols)
  ] 
    for r in range(n_rows)
]

If you were to "unroll" the inner one, it would look like

[
  [
    int(input("Enter value for {}. row and {}. column: ".format(r + 1, 1))),
    int(input("Enter value for {}. row and {}. column: ".format(r + 1, 2))),
    # ...
    int(input("Enter value for {}. row and {}. column: ".format(r + 1, n_cols-1))),
  ]
    for r in range(n_rows)
]

and further unrolling the outer one would result in

[
  [
    int(input("Enter value for {}. row and {}. column: ".format(1, 1))),
    int(input("Enter value for {}. row and {}. column: ".format(1, 2))),
    # ...
    int(input("Enter value for {}. row and {}. column: ".format(1, n_cols-1))),
  ],
  [
    int(input("Enter value for {}. row and {}. column: ".format(2, 1))),
    int(input("Enter value for {}. row and {}. column: ".format(2, 2))),
    # ...
    int(input("Enter value for {}. row and {}. column: ".format(2, n_cols-1))),
  ],
  # ...
  [
    int(input("Enter value for {}. row and {}. column: ".format(n_rows-1, 1))),
    int(input("Enter value for {}. row and {}. column: ".format(n_rows-1, 2))),
    # ...
    int(input("Enter value for {}. row and {}. column: ".format(n_rows-1, n_cols-1))),
  ]
]

Comments

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.