I'm new to Python, and while I've done some of the refresher on Codecademy, I'm having difficulty with this first task.
"Task 1: Please open the code skeleton file “i772_assg1.py” and implement the function list_ele_idx(li). You must write a “for loop” that reads the elements of a list, and for each element you create a tuple (element,index) to record the element and its index(position) in the list. This function takes a list “li” as its argument. The return value of the function should be a list of (element, index) tuples. In the main method, I have written a test case for task 1. Please un-comment the test case and test your function."
Here's what I'm trying, but I'm not getting anything. Any feedback will be greatly appreciated, as this is my first post on Stack Overflow!
def list_ele_idx(li):
index = 0 # index
element = [(5,3,2,6)]
li = [] # initialze a list; you need to add a tuple that includes an element and its index to this list
# your code here. You must use for loop to read items in li and add (item,index)tuples to the list lis
for index in li:
li.append((element, index))
return li # return a list of tuples
Let's walk through your code step by step so that you get an idea of the errors you are making, then let's look at a correct solution. Finally, let's look at the pythonic solution that might annoy your teacher.
The line
index = 0
is fine, because you want to start counting the indices at zero. The line
element = [(5,3,2,6)]
makes no sense, because your function should work for any given list, not just for your test case. So let's delete it. You initialize your result list with
li = []
which would be fine if you would not re-use the name of the given input list li
which is discarding the argument given to the function. Use
result = []
instead. Next your are looping over your now empty list li
with
for index in li:
and since li
is empty at this point, the loop body will never execute. Naming your loop variable index
is confusing because you are looping over the elements of a list with that syntax, not over the indices.
li.append((element, index))
Inside your for
loop is just wrong because you are never increasing index
and element
is a list, not a single element from your input list.
Here's a commented version of a working solution:
def list_ele_idx(li):
index = 0 # start counting at index 0
result = [] # initialize an empty result list
for item in li: # loop over the items of the input list
result.append((item, index)) # append a tuple of the current item and the current index to the result
index += 1 # increment the index
return result # return the result list when the loop is finished
Using enumerate(li)
would give you a simpler solution, but I don't think that's in the spirit of the exercise. Anyway, the short solution is:
def list_ele_idx(li):
return [(y,x) for x,y in enumerate(li)]
Check out Python's enumerate function, it gives you the element and its index to iterate over:
def list_ele_idx(li):
tuple_list = []
for index, item in enumerate(li):
tuple_list.append((index, item))
return tuple_list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With