taluka=[('sirur',),('jath',),('mauli',)]
queryset = data.objects.values_list("taluka").distinct()
queryset = [item[0] for item in queryset]
print(queryset) // sirur,jath,mauli,etc
Taluka=forms.ModelChoiceField(queryset=data.objects.values_list("taluka").distinct(),)
print(Taluka) // ('sirur',),('jath',),('mauli',)
I want the name of talukas to be printed as sirur,jath,mauli
CodePudding user response:
Solution 1: You can use the join function to convert the tuples inside the list into strings and then converting the list of strings into a comma separated string again using the join function.
taluka=[('sirur',),('jath',),('mauli',)]
taluka = ",".join(["".join(t) for t in taluka])
print(taluka)
Solution 2: Adding to the answer from LOCKhart, you can use list comprehension to convert the data into a list of strings and use the join function on it to convert it into a comma separated string.
taluka=[('sirur',),('jath',),('mauli',)]
taluka = ",".join([i[0] for i in taluka])
print(taluka)
CodePudding user response:
You could use list comprehension
taluka = [i[0] for i in taluka]
CodePudding user response:
Use .values_list("taluka", flat=True)
to get a flat list of a single field without useless 1-tuples. You should get ['sirur', 'jath', 'mauli']
Otherwise, list comprehensions are the way to go, as per LOCKhart's answer.