Home > Blockchain >  How to get the names from a tuple list
How to get the names from a tuple list

Time:10-30

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>

CodePudding user response:

You can get the values by index:

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

CodePudding user response:

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

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

CodePudding user response:

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])
  • Related