importing module inside of a list comprehension in python


The below program will show loading in console. Can we make it one line?
For that to happen we need to import time module inside of list comprehension.

How can we import module inside of list comprehension?

import time

[print(f"rLoading... " + (('|', '/', '-', '\')[i % 4]) + "t", end="") or time.sleep(0.10) for i in range(100) ]
Asked By: devp



You could do it like this but this is not a recommendation:

[print(time:=__import__('time'), f"33[2KrLoading... " + (('|', '/', '-', '\')[i % 4]) + "t", end="") or time.sleep(0.10) for i in range(100)] 
Answered By: Pingu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.