1

I am trying to print all the elements of a list using a user made function.

y = [1,2,3]
def ash(list1):
for x in range(len(list)):
    return list[x],x

what i want to do is return all the values in the list with their indices,but all i get is a single element.I can get the elements to print but not return.

stalepretzel
  • 15,543
  • 22
  • 76
  • 91
user2688772
  • 245
  • 1
  • 3
  • 7

4 Answers4

5

You are returning on the very first iteration. Rather, you need to create a list in your function, and add all the tuples in that list, and finally return that list.

And for getting both index and element, you can use enumerate():

def ash(list1):
    new_list = []
    for x, elem in enumerate(list1):
        new_list.append((elem,x))
    return new_list

Or, even better you can simply use List comprehension:

return [(elem, x) for x, elem in enumerate(list1)]

The previous two methods creates the list in memory. If you have a very large list to deal with, then you should probably use generators, using yield keyword:

def ash(list1):
    for x, elem in enumerate(list1):
        yield elem, x
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
2

enumerate(list) is what you're looking for. (see doc). Also, calling return will give you only the first value of the list when calling the function, what you want here is probably the yield statement:

def ash(list):

  for i,item in enumerate(list):
    yield item,i

if __name__ == '__main__':

  y = [1,2,3]

  ys_with_indices = list(ash(y)) 
  print ys_with_indices

Note that this will return a generator object, which you have to convert to a list by calling list() on it. Alternatively, just use a normal list that you append the individual values to:

def ash(list):

  items_with_indices = []

  for i,item in enumerate(list):
    items_with_indices.append((item,i))

  return items_with_indices

if __name__ == '__main__':

  y = [1,2,3]

  ys_with_indices = ash(y)
  print ys_with_indices
ThePhysicist
  • 1,822
  • 16
  • 21
2

Some issues with your code:

  1. Don't iterate using range unless necessary. Iterate the list directly, or here, use enumerate
  2. Don't use list as a variable - you'll shadow the built-in of the same name. It's confusing to the reader.
  3. You're returning out of the loop. This is why you only get the first iteration. If you want to return successive values, use yield, which turns your function into a generator:

    def ash(l):
        for x in range(len(l)):
            yield l[x],x
    
  4. This is really a reimplementation of enumerate:

    list(enumerate('abc')) #=> [(0, 'a'), (1, 'b'), (2, 'c')]
    
  5. If you really want to swap the order of the pairs, you can do:

    [b,a for a,b in enumerate('abc')]
    
  6. Alternative implementation: l='abc';zip(l,xrange(len(l)))

Marcin
  • 48,559
  • 18
  • 128
  • 201
0
def ash(lst):    
    return [(lst[x],x) for x in range(0,len(lst))]

You will get a list of tuples where the first value of the tuple is an element of the original list and the second the index of the element in the list. For y = [1,2,3] the result is [(1, 0), (2, 1), (3, 2)]

Lorenzo Baracchi
  • 1,808
  • 1
  • 13
  • 18