Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for loop execution
#1
I python documentation I found the below code and it's output -

>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
0 Mary
1 had
2 a
3 little
4 lamb


But in while I am practising in python 3.6 and going through the same code I am getting some different format output. The code along with the output is given below -

>>> word
['dog', 'window', 'cat', 'window']
>>> for i in range(len(word)):
... print(i,word[i])
...
(0, 'dog')
(1, 'window')
(2, 'cat')
(3, 'window')


Please ensure me which one should I follow, the documented one or the output that I am getting because it is of some different formatted.
Reply
#2
I can't reproduce your output.

$ python
Python 3.6.3 (default, Oct 24 2017, 14:48:20)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> words = ['dog', 'window', 'cat', 'window']
>>> for i in range(len(words)):
...     print(i, words[i])
...
0 dog
1 window
2 cat
3 window
>>> 
A refference link?[/i]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Python 3 produces the following:
>>> word = ['dog', 'window', 'cat', 'window']
>>> for i in range(len(word)):
...     print(i,word[i])
...
0 dog
1 window
2 cat
3 window
>>>
Python 2, on the other hand, produces this:
>>> word = ['dog', 'window', 'cat', 'window']
>>> for i in range(len(word)):
...     print(i,word[i])
...
(0, 'dog')
(1, 'window')
(2, 'cat')
(3, 'window')
>>>
If you are producing the second version you are not using python3 (even if you think you are).
Reply
#4
Since when the print in Python 2 is a function, not a statement?  I will try it right away Shy
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
That is the point, it isn't a function.
It is printing the parenthesis because syntactically you are asking it to print a tuple.
Reply
#6
Wow, the space between print and the parenthesis is missing and that confuses me.

Anyway, we do the same that way.

>>> words = ['dog', 'window', 'cat', 'window']
>>> for index, word in enumerate(words):
...     print index, word
...
0 dog
1 window
2 cat
3 window
https://docs.python.org/2/library/functions.html#enumerate
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to measure execution time of a multithread loop spacedog 2 2,864 Apr-24-2021, 07:52 AM
Last Post: spacedog
  How to to tie the execution of one process to another inside a loop in Python ignorant_wanderer 0 2,045 Jul-11-2020, 03:44 AM
Last Post: ignorant_wanderer
  A better way to limit loop execution? t4keheart 3 2,688 Feb-26-2020, 08:24 AM
Last Post: DeaD_EyE
  Time execution of a "for loop" with zip different in 2 equivalent context sebastien 1 1,910 Oct-11-2019, 11:07 AM
Last Post: sebastien

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020