Search code examples
pythonalgorithmfor-loopcycle

Getting ID field while using for cycle with objects


what is the best way to get for number each time it runs?

Code:

fields = []

objects = otherobjects.all() 
for object in objects
    fields.append(('id_is_#', object))

I want to put an ID to # place. ID would be generated ++ way.


Solution

  • fields = []
    objects = otherobjects.all() 
    
    for id, obj in enumerate(objects)
        fields.append(('id_is_' + id, obj))