Reputation: 106
sample i have a list:
sample = [a, b, c, d]
Then I want to pass the sample to the session:
self.session['sample'] = None #declaring the session...
for item in sample:
self.session['sample'] = str(self.session['sample']) + "," + str(item)
But the output is:
None, a, b, c, d
I want the value of my session['sample'] would be = a, b, c, d
Upvotes: 1
Views: 47
Reputation: 8935
You could do it in one line instead of a loop with join()
and list comprehension:
self.session['sample'] = ", ".join(str(item) for item in sample)
If you're happy doing it in a loop, you need to make the first item ""
instead of None:
self.session['sample'] = "" # Empty string
for item in sample:
self.session['sample'] += "," + str(item) # Note I've used += here
a += 1
is just a tidy way to write a = a + 1
Upvotes: 3