What do I add to result if using the range( ) method?


What do I add to result if using the range( ) method?


If you chose to write your loop with the range() method, you’ll need to access your list items with the current number in your range. In the code below, i holds the value of the current range value for our loop and we use that to access an index in our list.

for i in range(len(some_list)):
  print i  # This will print a number, starting at 0, going up to the list length
  result += some_list[i]  # Access the current value in our list
1 Like

Would it be correct to say that ‘for i in range(0, len(some_list):’, the i refers to each index. So you have to then use it in conjunction with the list to retrieve the value. And that the ‘for i in some_list:’ the i refers to the value itself?


@danyala Yes, that’s correct. You can test it yourself by running these in your Python interpreter:

  1. example_list = ['foo', 'bar', 'baz']
    # len(example_list) returns the number 3
    # so range(3) would produce the same result
    for i in range(len(example_list)):
    # output:
  2. example_list = ['foo', 'bar', 'baz']
    for i in example_list:
    # output:

2 posts were split to a new topic: Using an index to access items in a list of words

What is ‘i’? A variable? A command? Can it be replaced by another letter or is it a set command python is looking for?

1 Like

its indeed a variable. Its defined within the for loop, then python will assign the correct value to this variable each iteration of the loop

i is a commonly used name, its an “abbreviation” of index


So in this one how does Python know what ‘word’ is in this one?

n = [“Michael”, “Lieberman”]

Add your function here

def join_strings(words):
result = “”
for word in words:
result += word
return result

print join_strings(n)

Or lst here?

list_of_lists = [[1, 2, 3], [4, 5, 6]]

for lst in list_of_lists:
for item in lst:
print item

i explain this in my earlier answer, same answer with a slight modification:

lst and word are defined within the for loop definition, then python will assign the correct value to this variable each iteration of the loop

1 Like

Guys, just would like to know the last line in the code about printing is it right or not?

n = [3, 5, 7]

def total(numbers): #function
  result = 0 #variable
  for i in range(len(numbers)): #for i or any other name in range(len(of the argument that is in the function))
    result += numbers[i] #whenever it tells you add the amount to the result then this is the way, after the result was 0, now it is result = result + numbers from the argument(note that this is the second time to be used) and since we are dealing with a list then numbers[i] or any name we choose.
  return result
print total[n]

Have you tried running the code? If you get an error, the code is not right.

1 Like

Only the last line, the rest of the code is just fine.

then you answered your own question:

then the next question is why its wrong, any ideas?

well, it seems that when def is used, and wanted to be called, then print is not to be used.

No. The problem is that calling a function requires parentheses, the square brackets you use to access elements in string and list

the function requires parentheses to be called

1 Like

Do you have a specific question related to your code? We should refrain from posting full solutions or working code on the forums without questions associated with the code.

Welcome to the forums!

1 Like