473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Suggestion for the PythonDevelopment for next version

You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>for i in a:
print i
1
2
3
4

I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>for i in range(len(a)):
print a[i], i
1 0
2 1
3 2
4 3

I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>for i in a:
print i, i.index # i.index is my sugesstion
1 0
2 1
3 2
4 3

I think that this would be great and we cou pass by this annoying
"range(len(a))" functions
Oct 13 '08 #1
2 1119
azrael wrote:
You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>>for i in a:
print i
1
2
3
4

I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>>for i in range(len(a)):
print a[i], i
1 0
2 1
3 2
4 3

I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>>for i in a:
print i, i.index # i.index is my sugesstion
1 0
2 1
3 2
4 3

I think that this would be great and we cou pass by this annoying
"range(len(a))" functions
Luckily for those who read either the documentation or this form regulary,
the solution is already there (since python2.0 or so...)

for i, item in enumerate(iterable):
...

does the trick.

Diez
Oct 13 '08 #2
azrael a écrit :
You know, sometimes it annoys me to write a for loop in Python. If we
use a list a=[1,2,3,4], and want to loop through it, Python offers the
next option
>>>for i in a:
print i
1
2
3
4

I love this. So simple and smooth. But what happens if we need also
the position of an object in a list. Then there comes this annoying
writing.
>>>for i in range(len(a)):
print a[i], i
1 0
2 1
3 2
4 3

for index, obj in enumerate(any_sequence):
print "obj %s is at position %s" % (obj, index)

print "And I might read the FineManual(tm) some day..."

HTH !-)
Oh, and while we're at it:
I think that it would be great if the Python language, which is a
totaly OOP language, could also use the index variable from the first
example and consider it as an object with a Object variable. I mean
the following.
>>>>>for i in a:
print i, i.index # i.index is my sugesstion
Python is 100% OO in that everything (at least everything you can bind
to a name) is an object. But it doesn't mean everything has to be done
using the dotted syntax...
Oct 13 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brian van den Broek | last post by:
Hi all, There have been a few posts over the last month or so expressing a bit of exasperation with the "rising tide of newbie's". (Or, more accurately, the rising tide of questions from...
4
by: Stewart Midwinter | last post by:
I'm starting up a new website for a small community of local sports enthusiasts. I want to be able to create pages of information easily (i.e. a content management system), but also allow for...
0
by: Yoni Rapoport | last post by:
Hy everyone, just wanted to share a thought regarding UI. If you really think about it, events such as "Click", "DoubleClick", "KeyDown", "KeyUp", etc... (which clutter every winforms control we...
1
by: Andrew Ducker | last post by:
Almost every project I work on, I end up with some variant of an IResettable interface - so that controls can be reset to a default value. Controls have a ResetText method, but that's not much use...
8
by: | last post by:
I need source control to serve just one developer: me. Can someone suggest a system that would suit my needs? I am the only person writing code, and I manage the servers. Many of the SC solutions...
4
by: Bruce One | last post by:
Well folks, considering the high prices for the current VSTS MSDN Premium subscription, I propose we take a pause and look of what each one of the 3 possible package DOES NOT have: Team Edition...
0
ADezii
by: ADezii | last post by:
If you want to visit each item in an Array, you have two alternatives: Use a For Each..Next loop, using a Variant to retrieve each value in turn. Use a For...Next loop, looping from the Lower...
27
by: Mikhail Kovalev | last post by:
Hi. I work with recursive array trees of 10-20 levels in PHP. So far I have been using serialize() to store the arrays, generating files 5+ MB large, which take 10-15 seconds to unserialize and...
6
by: Jean-Paul Calderone | last post by:
On Mon, 13 Oct 2008 08:56:34 -0700 (PDT), azrael <jura.grozni@gmail.comwrote: Adding a random new attribute to arbitrary objects any time they happen to end up in a for loop would be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.