Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, is there an elegant way to print a list in a custom format without explicit looping?

Tags:

python

list

I know you can do

print str(myList) 

to get

[1, 2, 3] 

and you can do

i = 0 for entry in myList:   print str(i) + ":", entry   i += 1 

to get

0: 1   1: 2   2: 3     

But is there a way similar to the first to get a result similar to the last?

With my limited knowledge of Python (and some help from the documentation), my best is:

print '\n'.join([str(n) + ": " + str(entry) for (n, entry) in zip(range(0,len(myList)), myList)]) 

It's not much less verbose, but at least I get a custom string in one (compound) statement. Can you do better?

like image 356
stefaanv Avatar asked Dec 14 '10 15:12

stefaanv


People also ask

How do I print a list without a loop?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

How can I print a list without braces?

use asterisk '*' operator to print a list without square brackets.

How do you print all elements in a Python list without brackets?

You can print a list without brackets by combining the string. join() method on the separator string ', ' with a generator expression to convert each list element to a string using the str() built-in function. Specifially, the expression print(', '.

How do I print a list like a string in Python?

The most pythonic way of converting a list to string is by using the join() method. The join() method is used to facilitate this exact purpose. It takes in iterables, joins them, and returns them as a string.

How to print a list of elements without using loops in Python?

Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively. a = [1, 2, 3, 4, 5]

What happens when you call print() without arguments in Python?

As you just saw, calling print () without arguments results in a blank line, which is a line comprised solely of the newline character. Don’t confuse this with an empty line, which doesn’t contain any characters at all, not even the newline! You can use Python’s string literals to visualize these two: 'n' # Blank line '' # Empty line

What can you do with Python's print() function?

It helped you write your very own hello world one-liner. You can use it to display formatted messages onto the screen and perhaps find some bugs. But if you think that’s all there is to know about Python’s print () function, then you’re missing out on a lot!

How to print list in New lines or separated by space?

To print all elements in new lines or separated by space use sep=”n” or sep=”, ” respectively. # Python program to print list. # without using loop. a = [1, 2, 3, 4, 5] # printing the list using * operator separated. # by space. print(*a) # printing the list using * and sep operator.


2 Answers

>>> lst = [1, 2, 3] >>> print('\n'.join('{}: {}'.format(*k) for k in enumerate(lst))) 0: 1 1: 2 2: 3 

Note: you just need to understand that list comprehension or iterating over a generator expression is explicit looping.

like image 189
SilentGhost Avatar answered Sep 21 '22 16:09

SilentGhost


In python 3s print function:

lst = [1, 2, 3] print('My list:', *lst, sep='\n- ') 

Output:

My list: - 1 - 2 - 3 

Con: The sep must be a string, so you can't modify it based on which element you're printing. And you need a kind of header to do this (above it was 'My list:').

Pro: You don't have to join() a list into a string object, which might be advantageous for larger lists. And the whole thing is quite concise and readable.

like image 39
Manuzor Avatar answered Sep 18 '22 16:09

Manuzor