tags = ProductInStore.objects.get(id=product_in_store_id).product.tags.values_list('name', flat=True)
converted_list = list(tags)
tags_string = ''
for tags in converted_list:
tags_string = ',' tags
return tags_string
The output is
,tag1,tag2,tag3,tag4,tag5
but i'd like to get rid of the first comma. Do you have any idea how to do it?
CodePudding user response:
You can simply use the str.join(iterable)
method instead of the for loop:
tag_string = ",".join(tags)
CodePudding user response:
return tags_string.lstrip(",")
CodePudding user response:
return tags_string.strip(",")
Note: it will also remove "," (if any) from the end of string.
CodePudding user response:
You will have three approaches in front of you:
Option 1: Using for loop and enumerate
tags_string = ""
length = len(converted_list)
for index ,tags in enumerate(converted_list):
if index != length:
tags_string = tags ","
else:
tags_string = tags
tags_string
Option 2: Using lstrip
for tags in converted_list:
tags_string = ',' tags
tags_string.lstrip(",")
Option 3: Using join
",".join(converted_list)
All three approaches would result in the same output:
tag1,tag2,tag3,tag4,tag5