Home > Mobile >  Length of a list in a loop
Length of a list in a loop

Time:11-03

I was playing around with a small program that should find the average of numbers and the list of below, above average numbers in that list.


num = input("Enter a number: ")

total = 0
count = 0
lst = []

below_ave = []
equal_ave = []
above_ave = []

while num != '':
    n = int(num)
    total  = n
    count  = 1
    lst.append(n)
    num = input("Enter a number: ")

average = total // count

for i in range(0, len(lst)-1): # Here why it should be len(lst) instead of len(lst)-1
                               # I played around with it a bit, if i use len(lst)-1 then 
                               # the last number in the list (lst) will not be displayed
                               # But as you can see i tried to see if i made a mistake by 
                               # printing out each individual element of the list, lst[4] 
                               # does print out the last number, but it does not show up 
                               # in above_ave. 
    if lst[i] < average:
        below_ave.append(lst[i])
    elif lst[i] == average:
        equal_ave.append(lst[i])
    elif lst[i] > average:
        above_ave.append(lst[i])

print(lst[0])
print(lst[1])
print(lst[2])
print(lst[3])
print(lst[4])

print(f"lentgh of the list is {len(lst)}")

print(average)
print(below_ave)
print(equal_ave)
print(above_ave)
output
Enter a number: 5
Enter a number: 8
Enter a number: 6
Enter a number: 12
Enter a number: 9
Enter a number: 
5
8
6
12
9
lentgh of the list is 5
8
[5, 6]
[8]
[12]

Process finished with exit code 0

As commented in the program. I'm confused with the index of the element in a list. 0th element should represent the first element in a list, however, the output of the program does not show that. I have encountered this problem before, i ignored it but i don't think i should.

So when should one use len(list)-1 and len(list) in similar situations like mine? I usually use len(list)-1 and it works but sometimes it doesn't like in this code.

Appreciate the help!

CodePudding user response:

I personally never use len(lst) - 1 in python since if you take a look:

lst = [1, 2, 3, 4]
for i in range(len(lst)):
    print(lst[i])

Here i iterates through 0, 1, 2, 3 which would return the output being 1, 2, 3, 4. However, if you do len(lst) - 1 then you would not get the last element in the list i.e you wouldn't get i as 3 and the output as 4. range() simply omits the last integer passed.

For example:

for i in range(0, 10): # You can omit the starting 0 since it is default
   print(i) # Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Here as you can see you won't get 10 as the output since range() tends to omit the last value.

Using len(lst) - 1 is not recommended however you could use them if you have pre-defined integers that are co-related with your lst.

Improvement in Code: On Line 11 where you are checking if num != '' I would recommend to check if the input is a digit or no. Check it like: while not num.isdigit() The isdigit() is a default keyword which helps you check if all the values in a string are integers or no.

On your last line there is no need to use an elif condition to check if the value is greater than average. You can simply use else since all the above conditions aren't met so the last one meets by default.

You can also try surfing through list comprehension methods

CodePudding user response:

The length of your list is 5, correct.

However you do a len(list) - 1 your loop will run 1 iteration less, thus not display the last element that was stored at the end of the list which is 9.

You also printed out average twice with:

print(average)
print(equal_ave)

In other words: This is your list [5,8,6,12,9] With their positions respectively

lst[0] = 5
lst[1] = 8
lst[2] = 6
lst[3] = 12
lst[4] = 9

If your loop does not loop 5 iterations, because len(lst) = 5 and len(lst) - 1 = 4, your variable i will only loop 4 times which is 0,1,2,3

Thus not display the element 9 in your list!

CodePudding user response:

you need to use len(list) when you're going to iterate every element in the list.

And,

you need to use len(list) - 1 when you need every element except the last element in the list.

  • Related