0

Actually I already solved this problem but I want to solve it in another approach. I have a list [11,2,5,23,15,8,7,6,19,21,18] and variables

 pointer = 0
 number = list[pointer]

I iterated it using for loop, so for each number in that loop, I added the pointer by 1. So normally the variable number should change too right? because the pointer changed but it didn't change it still list[0] that is 11. do you guys know why did this happen? I believe there is something I didn't know about changing the variable

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • 3
    Can you please post your full code? – Jeff Huang Mar 08 '20 at 07:30
  • Python doesn't have pointers, and your code doesn't change anything. Please clearly explain what you are doing, what effect you are seeing and what you have expected instead. Note that ``list[pointer]`` immediately evaluates to a *value* that has no relation to ``list`` or ``pointer`` (i.e. ``number`` is not an expression). – MisterMiyagi Mar 10 '20 at 13:08
  • Does this answer your question? [Are python variables pointers? or else what are they?](https://stackoverflow.com/questions/13530998/are-python-variables-pointers-or-else-what-are-they) – MisterMiyagi Mar 10 '20 at 14:14
  • I am so sorry to all of you that tried to answer my question because i didnt explain it clearly. I already real all your answer and thank you. I understand more about python – Evan Ariawan Mar 11 '20 at 15:09

1 Answers1

-1

I can't diagnose your error without seeing your code, but here are 2 ways to do it. The first uses the manual increment method you described and the second takes advantage of an iterator:

my_list = [11,2,5,23,15,8,7,6,19,21,18]

#Parsing list by manual increment method:
x = 0    
while True:
    print(my_list[x])
    x += 1
    if x > len(my_list)-1:
        break
print('\n')

#This is how I would iterate through list:
for x in range(0,len(my_list)):
    print(my_list[x])
Scott
  • 367
  • 1
  • 9
  • 1
    The idiomatic way to iterate a list is ``for x in my_list``. Either way, that does not seem to be the goal of the question. – MisterMiyagi Mar 10 '20 at 13:11