I received this exercise:
Write a function
enumeratethat takes a list and returns a list of tuples containing(index,item)for each item in the list
My problem is that I cannot insert the index and value in one or a combination of for loops. This is the code I managed to make:
a = ["a", "b", "c","a","b","c"]
index = 0
for i in a:
print (index,i)
index+=1
This is roughly the code I want to produce (must be on one line):
my_enumerate = lambda x :[(t) for t in x )]
print list(my_enumerate(range(4)))
How can I put it all one lambda line to get (value, index) back? The output should look like:
[(0, "a"), (1, "b"), (2, "c")]
enumerate()function?my_enumerate = lambda L: list(ind,item for ind,item in enumerate(L))