Go through your code step by step so that you understand the mistakes you make, and then take a look at the right solution. Finally, let's look at a pythonic solution that can annoy your teacher.
Line
index = 0
excellent because you want to start counting indices to zero. Line
element = [(5,3,2,6)]
doesn't make sense because your function should work for any given list, not just your test case. So let me remove it. You initialize your list of results with
li = []
, li, , .
result = []
. li
for index in li:
li , . index , , .
li.append((element, index))
for , index, element - , .
:
def list_ele_idx(li):
index = 0
result = []
for item in li:
result.append((item, index))
index += 1
return result
enumerate(li) , , . , :
def list_ele_idx(li):
return [(y,x) for x,y in enumerate(li)]