-1

Can anyone please explain this?

userData = 10
emptyList = [0] * userData
for i in emptyList: 
    emptyList[i] = userData
    print(emptyList)
    userData -= 1

This in my mind, this code should do something different than it does.

what I am looking for- whatever the value is for userData, I would like it to be indexed in order value in the list emptyList.

This, I thought would give me the set [10, 9, 8] and so on... it doesnt.. it only changes the first variable in each iteration

what did I do wrong?

I made it work another way, userData = 10 emptyList = [] for i in range(userData): emptyList.append(i) userData -= 1 print(emptyList)

but thats not how I like it.. I need the 0 in that set to come out as 10 I think

user2962806
  • 47
  • 2
  • 7

5 Answers5

2
userData = 10
emptyList = [0] * userData
print emptyList

will print

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

and in the loop,

for i in emptyList:
    print i,

will print

0 0 0 0 0 0 0 0 0 0

So, in all the iterations of the loop, you are changing just the first element of the list. What you should actually have done is

userData = 10
emptyList = [0] * userData

for i in range(len(emptyList)):
    emptyList[i] = userData
    userData -= 1
print emptyList

Output

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

There is a builtin function called range which accepts starting value, ending value and the increment value and generates a sequence of values. With that function you can do this in a single line

print range(10, 0, -1)  #Python 2
print list(range(10, 0, -1))  #Python 3
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

The line:

for i in emptyList:

will give you a loop, setting i to each of the values in emptyList in turn.

Since all of those values are zero, that means i will always be zero, hence it will only ever change the first value.

If you want the list [10,9,8,7,6,5,4,3,2,1], just use the more Pythonic:

emptylist = [x for x in range (10, 0, -1)]
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Maybe you are reading the code like this

userData = 10
emptyList = [0] * userData
for i in range(len(emptyList)): # note range and len here
    emptyList[i] = userData
    print(emptyList)
    userData -= 1

You can just use

emptyList = range(userData, 0, -1)

or is it supposed to do something more?

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

The for loop is fundamentally different in Python than other languages - it's more of a for each loop (that is, for each element in the iterable, do stuff with it).

If you want to create a list of elements, use xrange for Python 2.x or range for Python 3.x:

emptyList = [i for i in xrange(10, 0, -1)]

emptyList = [i for i in range(10, 0, -1)]
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

You want something like

for i, j in emptyList:
  emptyList[j] = userData
  userData -= 1

Now you get both the value i (which you do not care for) and index j, which is what you need.

See Accessing the index in Python 'for' loops - you are not the first person struggling with loops in Python!

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122