So I have a list:
['x', 3, 'b'] And I want the output to be:
[x, 3, b] How can I do this in python?
If I do str(['x', 3, 'b']), I get one with quotes, but I don't want quotes.
When you wish to print the list elements in a single line with the spaces in between, you can make use of the "*" operator for the same. Using this operator, you can print all the elements of the list in a new separate line with spaces in between every element using sep attribute such as sep=”/n” or sep=”,”.
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.
In Python 2:
mylist = ['x', 3, 'b'] print '[%s]' % ', '.join(map(str, mylist)) In Python 3 (where print is a builtin function and not a syntax feature anymore):
mylist = ['x', 3, 'b'] print('[%s]' % ', '.join(map(str, mylist))) Both return:
[x, 3, b] This is using the map() function to call str for each element of mylist, creating a new list of strings that is then joined into one string with str.join(). Then, the % string formatting operator substitutes the string in instead of %s in "[%s]".
This is simple code, so if you are new you should understand it easily enough.
mylist = ["x", 3, "b"] for items in mylist: print(items) It prints all of them without quotes, like you wanted.
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