Is there a handy way to get an autoincrement or index into a list comprehension? I’m trying to build a list of objects, each of which needs a unique identifier. So instead of this:
tags_list = ['art', 'science', 'lollipops']
class Tag:
def __init__(self, id, name):
self.id = id
self.name = name
tags = []
i = 1
for t in tags_list:
tag = Tag(i, t)
tags.append(tag)
i += 1
…something like this:
tags = [Tag(i, t) for t in tags_list]
^^^ how do I get i
?
I know about enumerate()
but this gives me tuples right?
tags = [Tag(i, t) for i,t in enumerate(tags_list, 1)]