range(len(marks_data))
with enumerate(marks_data)
246 try:
247 all_marks = []
248 all_weightage = []
249 for i in range(len(marks_data)):250 student_marks = StudentMarks.objects.get(
251 marks_type=marks_type, scsddc=scsddc, pk=marks_data[i]['id'])
252 print(student_marks)
Using range(len(...))
is not pythonic. Python does not have not index-based loops. Instead, it uses collection iterators.
Python has a built-in method enumerate
which adds a counter to an iterable.
Using this, you can access the counter and the value from the iterable at the same time.
It is therefore recommended to replace range(len(...))
with enumerate(...)
.
for index in range(len(mylist)):
...
for index, element in enumerate(mylist):
...