importing module inside of a list comprehension in python

117 Views Asked by At

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) ]
1

There are 1 best solutions below

1
SIGHUP On BEST ANSWER

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

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