Reputation: 29
I am currently trying to print the results of list using a for loop in python, but I want the last number to print a space, then a newline. for ex.
hourly_temperature = [90, 92, 94, 95]
for num in hourly_temperature:
print(num, '-> ', end= '')
I'm getting 90 -> 92 -> 94 -> 95 ->
but i want 90 -> 92 -> 94 -> 95
I've tried using len
and range
with if
else
statemets, but i didt really work.
Upvotes: 0
Views: 98
Reputation: 488
If you want to use a for loop you can loop over every element except for the last one, and then print the last one manually, like this:
for num in hourly_temperature[:-1]:
print(num, '->', end='')
print(num[-1])
It might be simpeler to use join though, for example:
strs = [str(x) for x in hourly_temperature] #convert values to str
print('->'.join(strs))
This converts your list to string with a '->' between every two elements.
Upvotes: 1