Hello all,
So I have a tuple which is attained from my views.py
document and is called as a for
loop from my template (HTML Template).
This all works perfectly & as expected.
However, I want to take this one step further and put the word "and"
in between the second from last and the last item in the tuple. i.e.
> United Kingdom, Germany, United States, France and Canada
I currently have the following code. This is the code which is called as a for
loop from my HTML template.
def legal__privacy_statement(request):
...
...
...
context = {
...
....
....
'datacountry' : ['United Kingdom','Germany','United States', 'France', 'Canada'],
....
}
return render (request, 'privacy-statement.html', context)
Playing around with this, I am thinking of separating the last item in the tuple, placing the word “and” & then appending the last tuple item on the end. I’m thinking something like:
'datacountry' : ['United Kingdom', 'Germany', 'United States', 'France', 'Canada'],
a = datacountry[ : -1 ]
b = datacountry[ - 1 ]
c = a + "and" + b
return{c}
So I will get the result:
United Kingdom, Germany, United States, France, and Canada
However, Python is not McLoving the fact that I am mixing a string and a tuple together.
Is there a way which I’m missing here in which I can place and “and” into the tuple?
Many thanks.