Ziggy Witkowski
Ziggy Witkowski

Reputation: 81

To avoid for loop and print elements in list in new line

I try to avoid the for loop when printing list items.

Done few attempts to do that but always wrong. Example:

print('\n*'.join(i) for i in li)
<generator object <genexpr> at 0x7ff0c0e8b900>

I want equivalent of that:

    li = ['apple', 'beetroot', 'cabbage']
    
    for e in li:
        print(f'*{e}')
*apple
*beetroot
*cabbage

Upvotes: 0

Views: 33

Answers (1)

ddg
ddg

Reputation: 1098

If you want, you can do this with array unpacking and a list comprehension. Although personally I would stick with the for-loop for readability.

 print(*[f'*{e}' for e in li], sep='\n')

Upvotes: 1

Related Questions