I have a list L
of elements, say natural numbers. I want to print them in one line with a single space as a separator. But I don't want a space after the last element of the list [or before the first].
In Python 2, this can easily be done with the following code. The implementation of the print
statement [mysteriously, I must confess] avoids to print an extra space before the newline.
L = [1, 2, 3, 4, 5]
for x in L:
print x,
print
However, in Python 3 it
seems that the [supposedly] equivalent code using the print
function produces a space after the last number:
L = [1, 2, 3, 4, 5]
for x in L:
print[x, end=" "]
print[]
Of course there are easy answers to my question. I know I can use string concatenation:
L = [1, 2, 3, 4, 5]
print[" ".join[str[x] for x in L]]
This is a quite good solution, but compared to the Python 2 code I find it counter-intuitive and definitely slower. Also, I know I can choose whether to print a space or not myself, like:
L = [1, 2, 3, 4, 5]
for i, x in enumerate[L]:
print[" " if i>0 else "", x, sep="", end=""]
print[]
but again this is worse than what I had in Python 2.
So, my
question is, am I missing something in Python 3? Is the behavior I'm looking for supported by the print
function?
Write a Python program to print a list of space-separated elements.Python List: Exercise - 62 with Solution
Sample Solution:-
Python Code:
num = [1, 2, 3, 4, 5]
print[*num]
Sample Output:
1 2 3 4 5
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to create a list of empty dictionaries.
Next: Write a Python program to insert a given string at the
beginning of all items in a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Checking whether all elements in the sequence are Truthful:
>>> all[a % 2==0 for a in range[0,10,2]] True
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- JavaScript basic - Exercises, Practice, Solution
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- JavaScript conditional statements and loops - Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools
- JavaScript: HTML Form Validation
To convert a space-separated string to a list in Python, call the Output: This works because the Let’s then take a look at how to split a
string of integers into a list of integers.str.split[]
method:sentence = "This is a test"
words_list = sentence.split[]
print[words_list]
['This', 'is', 'a', 'test']
str.split[]
splits the string by blank spaces by default.
To convert a space-separated string of integers to a list in Python:
- Split the string on empty spaces.
- Convert each element to an integer.
- Add each integer to a list.
You can do this with a for loop:
numbers_str = "1 2 3 4 5" numbers_list = [] for num_str in numbers_str.split[]: num_int = int[num_str] numbers_list.append[num_int] print[numbers_list]
Output:
[1, 2, 3, 4, 5]
To make the expression way shorter, you can use a list comprehension:
numbers_str = "1 2 3 4 5" numbers_list = [int[num] for num in numbers_str.split[]] print[numbers_list]
Output:
[1, 2, 3, 4, 5]
Conclusion
Thanks for reading. I hope you found the answer you were looking for.
Happy coding!
Further Reading
Python Tricks
Python List Comprehensions