Search code examples
pythonone-liner

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

Solution

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