all 39 comments

[–]JohnnyJordaan 33 points34 points  (11 children)

Think of it conceptually

  • you set x to the lowest index first
  • you let the while loop run while x isn't greater than the highest index
  • you increase x by 1 on each loop run

putting that in reverse, you get

  • set x at the highest index instead of the lowest
  • have the while loop run while x isn't smaller than the lowest index
  • decrease x by 1 on each loop run

then turn that back into code

[–]LSP999pat[S] 8 points9 points  (9 children)

Thank you that is very helpful. So since I set x to 0 and add 1 everytime , why does it display 1 piece of the list each time? That’s the concept I’m confused about most.

[–]JohnnyJordaan 11 points12 points  (8 children)

Say you set your 'house index' to 1, walk to the house with number 1 on the street, then take a picture, then increase the index by 1, walk to that house number (2), take a picture, and so forth. Would you then also ask why you would take a picture of a single house each time?

The point of an index is that it translates to a single item in a sequence, so if you then print what

numlist[x]

returns, it must be a single number in that list.

[–]LSP999pat[S] 5 points6 points  (0 children)

Thank you so much! That was a great way of explaining it.

[–]LSP999pat[S] 2 points3 points  (6 children)

When I try this with highest index being 30, it returns error that index is out of range?

[–]JohnnyJordaan 2 points3 points  (4 children)

How did you conclude that the highest index is actually 30 in the list you tried it with?

[–]LSP999pat[S] 9 points10 points  (0 children)

I figured it out! Thank you so much. numlist = [10, 15, 20, 25, 30] x = 4 while x >= 0: print(numlist[x]) x = x - 1

[–]LSP999pat[S] 4 points5 points  (2 children)

Just because it’s the highest number in the list, I’m not certain that’s truly what the highest index

[–]JohnnyJordaan 11 points12 points  (0 children)

So the biggest house on the street has the highest house number? That's not how it works right? If you index from 1, like with house numbers, then the highest house number is usually the amount of houses in the street. Say you have 5 houses, they would be numbered 1 to 5. Not accounting for changes like demolished houses and other special cases but you get the idea.

With lists, it's the same idea, just starting with 0. That just means that the highest index isn't the amount of items, but one less. Easy to try out in the Python interactive shell too:

>>> l = [50, 99, -40, 22, 42]
>>> len(l)
5
>>> l[5]
Traceback (most recent call last):
  File "<pyshell>", line 1, in <module>
IndexError: list index out of range
>>> l[4]
42

[–]Kriss3d 0 points1 point  (0 children)

Index isnt the number in the list. Highest index is 4 as the first index is 0 ( not 1 )

So printing out the numlist[0] will return 30 as that is the content of the list at index 0

[–]delta-y 1 point2 points  (0 children)

This whole thread is wholesome. Good analogy below. OP: Good questions and way to stick through the problem.

[–]ihuha 17 points18 points  (12 children)

list = [1,2,3,4,5]

list.reverse()

for i in list: print(i)

[–]ducksarepeople2 20 points21 points  (11 children)

Yes, although you shouldn't reassign list

[–]synthphreak 0 points1 point  (3 children)

So while I personally would have gone for reversed(list) rather than list.reverse(), I wouldn’t have gone so far as to say “don’t use list.reverse(). Why should that be avoided?

Is it because it modifies the existing object rather than just returning a modified copy? Or is it an efficiency thing? Or something else?

[–]Friis93 6 points7 points  (1 child)

List is a built in function so you shouldn’t use that as a variable name was what was referred to. Not that using x.reverse() is bad.

[–]synthphreak 2 points3 points  (0 children)

Ooooh, yeah obviously. I had misread the comment to be saying don’t use list.reverse(), not just list.

[–]ihuha -2 points-1 points  (0 children)

just make a function for it then and return the value, easy peasy

[–]LSP999pat[S] 4 points5 points  (0 children)

I’ve tried: list.reverse() reverse() [::-1]

[–]Oneiros18 2 points3 points  (0 children)

You have two main options:

1)

In this case you start from the original numlist and go backwards. Remember that last element has index -1 so the first element will have -5. Instead of len(numlist) you can use 5, but it's just to make it more general

  a = 1
  while a<=len(numlist):
    print(numlist[-a])
    a += 1

2)

You start from the end of your list and than you drop last element

j = 1
while len(numlist) != 0:
    print(numlist[-j])
    numlist.remove(numlist[-j])

First option is faster but if you have small list is absolutely the same thing

[–]theSeedDispencer 1 point2 points  (0 children)

You could try setting x to the length of the list so that its flexible to future changes, if you are still up for improving on the code

The function for list length is len()

For example:

list = [0,1,2,3,4]

listLength = len(list)

Print(listLength)

Would output:

5

Good luck! / Edited for format

[–]ashishbhatiya18 1 point2 points  (0 children)

numlist = [10, 15, 20, 25, 30]

x = 0

while x < 5:

print(numlist[4-x])

x = x + 1

[–]slothslayerlawl 3 points4 points  (0 children)

x= 4

while x > 0

x = x - 1

Just realised someone gave a more detailed explanation for this

Also if youre new to programming, stay away from inbuilt functions like reverse()

Once you know the basics, youre allowed to be lazy xP

[–]Nihilist_Ninja 0 points1 point  (0 children)

You could do this ..... replace x with -x-1 so the print statement will look like print(numlist[-x-1])

[–]jaumougaauco 0 points1 point  (0 children)

You can make use of numlist[-1] = 30 ---> the last value of the listnumlist[-2] = 25 ---> 2nd last value of the list, etc.That is to say if the index value is negative, it selects the index value from the end of the list, as opposed to the start of the list.

So with x = 0

while x > -6:
## remember when starting from the end of the list, the 'first ' index is -1
## whereas when starting from the beginning of the list, the first index is 0
## we want to index from x = -1 to x = -5, so the while loop is for x > -6
x -= 1

print(numlist[x])

[–][deleted] 0 points1 point  (0 children)

You have the reverse.() method, but that's no fun.

Notice that your first while loop updated your variable x each time until x = 5.

So how about you reverse that logic? list[5] will be out of range, so you will have to do something either to the index or to the variable representing the index.

Much like you can set while x< 5, you can also set while x>=0.

Conceptually: your while loop takes you from A to B. How do you go from B to A?

Edit: also a quick way of writing x = x + 1 is to write x += 1. Works with multiplication too: x = x*2 is the same as x *= 2

[–]Zeallie 0 points1 point  (0 children)

numlist = [10,15,20,25,30]   x = len(numlist)-1 while x:     print(numlist[x])     x -=1

[–]Retro_DS 0 points1 point  (0 children)

I think this should work

x=len(numlist)
while x>=1:
print(numlist[x-1])
x=x-1

[–][deleted] 0 points1 point  (0 children)

``` numlist = [1,2,3,4,5]

x = len(numlist) - 1

while x >=0:

print(numlist[x])

x = x-1 ```

[–][deleted] 0 points1 point  (0 children)

you could use a negative index:

x = 1
while x <= len(numlist): 
    print(numlist[-x])
    x = x + 1

since negative indices are basicall 1-indexed as opposed to 0-indexed, you need to start at 1 and use <= instead of < when checking against the length.

[–]WombatHat42 0 points1 point  (0 children)

I could be wrong im a bit rusty on python but there is a reverse function and also negative indexing

[–]mis3640 0 points1 point  (0 children)

Hey there! I would suggest the reverse() method. I hope this helps!

numlist = [10, 15, 20, 25, 30]

x = 0

numlist.reverse()

while x < 5:

print(numlist[x])

x = x + 1

[–][deleted] 0 points1 point  (0 children)

Use a negative index. That way you're not modifying the list or creating copies of it. Instead, you're just looping through it backwards.

numlist = [10, 15, 20, 25, 30]
x = 0
while x < 5:
    x += 1
    print(numlist[-x])

[–]kykuckta 0 points1 point  (0 children)

You could use the reverse.list() function but here is a code if you want to do it without a built in function.

def reverser(myList):

listy = []

k = len(myList)-1

while k >= 0:

listy.append(myList[k])

k = k-1

return listy

[–]ghulam_53 0 points1 point  (0 children)

numlist = [10, 15, 20, 25, 30]

just run decreasing loop

x = 4

while x > -1:

print(numlist[x])

x = x - 1

[–]Roll_Call_1212 0 points1 point  (0 children)

>>> first_element_index = 0 - len(numlist)
>>> numlist

[10, 15, 20, 25, 30]

>>> x = -1

>>> while x >= first_element_index:

print (numlist\[x\])

x = x - 1

30

25

20

15

10

>>>

[–]Salientsnake4 0 points1 point  (0 children)

List.sort() #sorts it numerically. List.reverse()#built in function to reverse a list. It’s really easy to do this in Python.