Lists and Functions: 18. Using a list of lists in a function


#1

Using a list of lists in a function

Hi all,

I've gotten the code to run correction in the Using a list of lists in a function exercise (as posted below), but I had a question as to how the code works.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in range(len(lists)):
        for i in lists[numbers]:
            results.append(i)
    return results

print flatten(n)

I've been using the range(len(list) method for iteration for practice purposes. The area that I'm still wrapping my head around is why in an example like this...

for i in range(len(list)):
    print list[i]

...I need to use list[i] instead of just "i".

In relation to this Lists and Functions assignment, if I was to use the first list method, the code would look like this:

def flatten2(lists):
    results = []
    for numbers in lists:
      for i in numbers:
        results.append(i)
    print results

My question is, why doesn't...

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = []
    for numbers in range(len(lists)):
        for i in numbers:
            results.append(i)
    return results

print flatten(n)

...work with the range(len(list)) module of iterating?


#2

range will give you numbers which you can use to access the items in the loop, look:

lst = ['a', 'b', 'c', 'd', 'e']
for i in range(len(lst)):
   print i, lst[i]

this indexes you can use to access the items in the list. if you just use a regular for in loop, the items gets assigned to the iterator (in this case i):

lst = ['a', 'b', 'c', 'd', 'e']
for i in lst:
   print i

there is the difference


#3

Okay, I think I get that part - range(len(1st)) turns i into a index value rather than the item itself.

However, I don't understand why an earlier lesson states that this method is the only way to modify a list.

For example:

list = ['badger','walrus','rhino']

def test(x):
  for i in range(len(x)):
    print x[i].upper()

test(list)

def test2(x):
  for i in x:
    print i.upper()

test2(list)

Both of these print out the same thing


#4

but neither manipulate the list? look:

lst1 = ['badger','walrus','rhino']
lst2 = ['badger','walrus','rhino']

def test(x):
  for i in range(len(x)):
    print x[i].upper()

test(lst1)
print lst1

def test2(x):
  for i in x:
    print i.upper()

test2(lst2)
print lst2

i made two separate list, just to be sure. Yes, you can use range to manipulate the list:

lst1 = ['badger','walrus','rhino']
def test(x):
  for i in range(len(x)):
    x[i] = x[i].upper()
    print x[i]
test(lst1)
print lst1

see how you can manupilate the list with range? You can't do this without range.

Of course, if you loop over it without range, you can temporary print an uppercase version (the value of the list is copied into the iterator) and then displayed in uppercase, but it doesn't manipulate the list


#5

Huh... and why is it that the function below doesn't alter the list, but your range(len(x)) example does?

lst1 = ['badger','walrus','rhino']
def test(x):
  for i in x:
    i = i.upper()
    print i
test(lst1)
print lst1

#6

because with this loop:

for i in x:

a copy of the item in the list is stored in i (for each iteration of the loop). you can't alter the list with this, that is why you use range, range gives indexes, which we can use to manipulate the list.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.