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?
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.
use asterisk '*' operator to print a list without square 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(', '.
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.
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]
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
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!
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.
>>> 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With