Python Forum
Loops in List - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Loops in List (/thread-2370.html)

Pages: 1 2


Loops in List - SandraDan - Mar-10-2017

Hello everyone,

I'm new to Python and I'm having difficulty understanding why my Loop-List code doesn't work.

# loop in list

listA = list(range(0,101,5))

for i in range(len(listA)):
    print('Item no. ' + str(i+1) + ' is: ' + listA[i])
Please advise. Thank you.  Big Grin


RE: Loops in List - wavic - Mar-10-2017

listA = list(range(0,101,5))

for index, element in enumerate(listA, 1):
    print("Item no. {} is {}".format(index, element))



RE: Loops in List - Skaperen - Mar-11-2017

this is showing the perl philosophy that there is more than one way to do it.  so why not put str() around listA[i] so the original method works as intended?  what is wrong with minimal change?

# loop in list
 
listA = list(range(0,101,5))
 
for i in range(len(listA)):
    print('Item no. ' + str(i+1) + ' is: ' + str(listA[i]) )
[/i][/i]


RE: Loops in List - wavic - Mar-11-2017

It's not wrong. print('Item no. ', (i+1), ' is: ', listA[i]) will work too. It's about routines. 

Hello, @SandraDan. As @Scaperen showed you, to concatenate strings they must be strings. listA[i] is an integer.


RE: Loops in List - buran - Mar-11-2017

(Mar-11-2017, 08:04 AM)Skaperen Wrote: what is wrong with minimal change?
well, why not show a better way, 'pythonic' why?


RE: Loops in List - Skaperen - Mar-12-2017

(Mar-11-2017, 08:29 AM)wavic Wrote: It's not wrong. print('Item no. ', (i+1), ' is: ', listA[i]) will work too. It's about routines. 

Hello, @SandraDan. As @Scaperen showed you, to concatenate strings they must be strings. listA is an integer.

it did not work, unchanged, for me.  i think it should have, but i definitely got an error.

Output:
lt1/forums /home/forums 6> cat foo.py # loop in list   listA = list(range(0,101,5))   for i in range(len(listA)):     print('Item no. ' + str(i+1) + ' is: ' + listA[i]) lt1/forums /home/forums 7> py3 foo.py Traceback (most recent call last):   File "foo.py", line 6, in <module>     print('Item no. ' + str(i+1) + ' is: ' + listA[i]) TypeError: Can't convert 'int' object to str implicitly lt1/forums /home/forums 8>
wrapping it in str() made it work.  it had been my understanding that print() would apply repr() to any non-string argument it got.  i learned i was wrong.

(Mar-11-2017, 08:50 AM)buran Wrote:
(Mar-11-2017, 08:04 AM)Skaperen Wrote: what is wrong with minimal change?
well, why not show a better way, 'pythonic' why?

if that is the direction we want newbies to go in very early then we should explain it in all cases where a non-pythonic way fails and working pythonic code is given.  and explaining why [i]that code is pythonic would be a plus.

my look on it is what did the poster do wrong.  if what the poster did wrong was not code in a pythonic way, then that should be said.

i highlighted "that" and clicked on the italicize button.  it showed up for me correctly in the edit window and in the preview.  then when i posted it seems to have lost the closing tag ... maybe a bug in post merging?


RE: Loops in List - wavic - Mar-12-2017

@scaperen, you are right. print('Item no. ', (i+1), ' is: ', listA[i]) works if the list is created without the 'start' and 'step' parameters because the loop gets the value. So the list has 21 elements and after the 5th iteration, the next value is 25 and if we use it as an index... well, it prints index out of range error. 

Your error message is strange  Cool 

Anyway! I use print(var1, var2, etc.) only for testing. Why I've used str.format() in my example? Because when one does the things in one way he/she create a habit. I've seen it in myself.


RE: Loops in List - ichabod801 - Mar-12-2017

(Mar-12-2017, 04:43 AM)Skaperen Wrote: it had been my understanding that print() would apply repr() to any non-string argument it got.

It does. The error happens before that point. First it evaluates the argument it gets, then it applies repr. The error comes in the evaluation, when + listA[i] tries to add an integer to a string.

Edit: Didn't think it through. print applies str() to any non-string argument it gets. Typing an argument into the interactive console gives the repr().


RE: Loops in List - SandraDan - Mar-12-2017

(Mar-10-2017, 09:30 PM)wavic Wrote:
listA = list(range(0,101,5))

for index, element in enumerate(listA, 1):
    print("Item no. {} is {}".format(index, element))

@wavic I don't understand this version of the code. I would have to learn more about Python to be able to grasp this. But thank you very much for your advice.  Wink

(Mar-11-2017, 08:04 AM)Skaperen Wrote: this is showing the perl philosophy that there is more than one way to do it.  so why not put str() around listA[i] so the original method works as intended?  what is wrong with minimal change?

# loop in list
 
listA = list(range(0,101,5))
 
for i in range(len(listA)):
    print('Item no. ' + str(i+1) + ' is: ' + str(listA[i]) )
[/i][/i]

@Skaperen yeah this worked! Thanks so much!


RE: Loops in List - wavic - Mar-12-2017

(Mar-12-2017, 05:22 PM)SandraDan Wrote: @wavic I don't understand this version of the code. I would have to learn more about Python to be able to grasp this. But thank you very much for your advice.
This is called string formatting. {} is where the positional arguments in the string format() method is replaced with.
Here is a simple example:

a = 'one'
b = 'two'
c = 'three'

print("Count to 3:\n{}\n{}\n{}".format(a, b, c))
Output:
Count to 3: one two three