0

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
  • Does this answer your question? [How can I format a list to print each element on a separate line in python?](https://stackoverflow.com/questions/13443588/how-can-i-format-a-list-to-print-each-element-on-a-separate-line-in-python) – Tomerikoo Feb 12 '21 at 17:11
  • If you are using `join` you don't need to loop the list at all: `print('\n*'.join(li))` but this will leave the first item, without a `*`. Can be solved with `print('*' + '\n*'.join(li))` – Tomerikoo Feb 12 '21 at 17:13

1 Answers1

1

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')
ddg
  • 1,090
  • 1
  • 6
  • 17