LIST Comprehension | Sololearn: Learn to code for FREE!


LIST Comprehension

I have this code 👇👇👇 1.n=int(input()) 2.for i in range(n+1): 3. for j in range(11): 4. print(i,"*",j,"=",i*j) 5. print("---------------------") & I want implement the above code in this format n=int(input()) [[print(i,"*",j,"=",i*j) for i in range(11)] for j in range(n+1)] But I'm not getting the last print ("------") statement, how to do?

5/5/2020 4:05:02 PM

Prince Kumar

10 Answers

New Answer


n = int(input()) [[print(*(i, "*", j, "=", i * j) if j < 11 else "---------------------") for j in range(12)] for i in range(n+1)] Best I could do...still looking into it though.


I think, most of the Parterre u already know. print("a", 0* "b") -->a print("a", 1* "b") -->ab print("a", True* "b") -->ab print("a", False* "b") -->a print("a", (3==3)* "b") -->ab print("a", (3==4)* "b") -->a print("a", (3==3)* "-------") -->a------- print("a", (3==3)* "/n-------") --> a ------- it is almost same Think as rodwynnejones did. so if i==10 the block is finished and you add the line.

+3 the "----------" outputs correctly:- n = int(input()) [[print(("{} * {} = {}".format(i, j, i * j)) if j < 11 else "--------------") for j in range(12)] for i in range(n+1)]



Oma Falk will you explain me ,it's working


rodwynnejones thanks buddy I want to know about what do we call this format and some more details


@𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Some sort of list comprehension? Maybe someone else can clarify.


Oma Falk yeah I understood your explanation & I appreciate it But as it doesn't match to my code So will you give me, your explanation (above one)small snippet so that I can clear my doubt


Tomiwa Joseph how to add 5. Line in 2nd code?


rodwynnejones yeah I need explanation of this stuff