Kyle
Kyle

Reputation: 309

How to get the names from a tuple list

I currently have a list of Names in a tuple list and when they are displayed on my form, they are displayed like so:

('Name1','Name1'),
('Name2','Name2'),
('Name3','Name3'),

Which is exactly how they are shown in the code.

How would I get them to display like so:

Name1 
Name2
Name3

My code currently looks like so

Views.py

def customerDetails(request):
    model = complex_list

    content = {'model': model}
    return render(request, 'main/customerDetails.html', content)

template.html

<form>
 <<select  name="Complex">
    {% for x in model %}
        <<option value="{{ x }}">{{ x }}</option>
    {%  endfor %}
  </select>> Complex

</form>

Upvotes: 0

Views: 838

Answers (3)

Lakpa Tamang
Lakpa Tamang

Reputation: 428

Suppose your list looks like this:

namelist = [('Name1','Name1'),('Name2','Name2'),('Name3','Name3')]

It is a list of tuples. You can access the value of tuple by first looping inside the list and then printing the index of the 1st element of that list (i.e., tuple):

for name in namelist:
print(name[0])

Upvotes: 1

smyril
smyril

Reputation: 128

mynames = [('name1', 'name1'), ('name2', 'name2'), ('name3', 'name3')]

for i in mynames:
  print(i[0])

Upvotes: 1

lucutzu33
lucutzu33

Reputation: 3700

You can get the values by index:

{% for x in model %}
    <option value="{{ x.0 }}">{{ x.1 }}</option>
{%  endfor %}

Upvotes: 1

Related Questions