+ 4

Python One Liner

Can this program be written in one line? If yes, how? for i, j in zip(range(1, 11), range(1, 12)): print(i,'miles = ',round(j*1.609,3),'km') I did this parallel iteration using zip() but don't know how to unpack when writing one line as this single iteration can be written though. print(*[i for i in range(1,11)])

16th Jul 2020, 4:14 AM
Ankit Yadav
Ankit Yadav - avatar
3 odpowiedzi
+ 5
You don't need the zip() function, a basic comprehension is sufficient. You even can save the round() function, this can be done like shown in the code with a format specifier. [print(f'{i} miles = {i*1.609:.3f} km') for i in range(1,11)]
16th Jul 2020, 9:49 AM
Lothar
Lothar - avatar
+ 4
thanks 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 i didn't thought of the way that it can print directly if it is in list thanks a lot again
16th Jul 2020, 4:23 AM
Ankit Yadav
Ankit Yadav - avatar
+ 3
thanks a lot Lothar i like the way 👍
18th Jul 2020, 1:41 PM
Ankit Yadav
Ankit Yadav - avatar