473,789 Members | 2,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for loop without variable

Hi. I'd like to be able to write a loop such as:
for i in range(10):
pass
but without the i variable. The reason for this is I'm using pylint
and it complains about the unused variable i. I can achieve the above
with more lines of code like:
i = 0
while (i != 10):
i += 1
Is there a concise way to accomplish this without adding extra lines
of codes? Thanks in advance for your help.
Jan 9 '08
35 21585
On Fri, 11 Jan 2008 01:48:43 -0500 Marty <ma*****@earthl ink.netwrote:
Mike Meyer wrote:
This caused me to wonder why Python does not have a "foreach" statement (and
also why has it not come up in this thread)? I realize the topic has probably
been beaten to death in earlier thread(s), but does anyone have the short answer?
But I'm curious - what's the difference between the "foreach" you have
in mind and the standard python "for"?
For example, I thought the python "equivalent " of perl's foreach might be:

No, python's equivalent of Perl's foreach is "for". I.e.

foreach $var (@list)

does the same thing as Python's

for var in list

(except Perl gets the scoping right).

Maybe you're thinking of Perls "default variable" feature (I don't
know what else to call it), which implicitly uses $_ as a variable in
any number of places if you fail to provide a variable? So that you
can say:

foreach (@list)

and apparently not have to use a variable, except it implicitly uses $_.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 11 '08 #31
Paul Rubin wrote:
it just seems way too obscure though. Python style seems to favor
spewing extra variables around.
that's because (local) variables have near-zero cost, and zero overhead.
use as many as you want, and reuse them as often as you want to.

(and if you use sane naming conventions, the risk for collisions is near
zero as well).

</F>

Jan 11 '08 #32
Fredrik Lundh <fr*****@python ware.comwrites:
(and if you use sane naming conventions, the risk for collisions is
near zero as well).
I haven't felt that way, I'm always worried about clobbering something
by leaking a variable. Maybe collisions don't really happen much, but
it's always seemed cleaner to me to use the most restricted scopes
possible just to minimize or eliminate the possibility. This is
especially attractie in a language like Python, with no declarations
and no compile-time type safety.
Jan 11 '08 #33
Marty:
I recently faced a similar issue doing something like this:

data_out = []
for i in range(len(data_ in)):
data_out.append ([])
Another way to write this is
data_out = [[]] * len(data_in)

Neil
Jan 11 '08 #34
On Fri, 11 Jan 2008 22:18:22 GMT Neil Hodgson <ny************ *****@gmail.com wrote:
Marty:
I recently faced a similar issue doing something like this:
data_out = []
for i in range(len(data_ in)):
data_out.append ([])

Another way to write this is
data_out = [[]] * len(data_in)
Not quite. All the other variants give you 23 empty lists. That one
gives you 23 references to one list:
>>x = [[] for _ in range(23)]
x[1].append(23)
x
[[], [23], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]
>>x = [[]] * 23
x[1].append(23)
x
[[23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23], [23]]
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/consulting.html
Independent Network/Unix/Perforce consultant, email for more information.
Jan 11 '08 #35
>I recently faced a similar issue doing something like this:
>>
data_out = []
for i in range(len(data_ in)):
data_out.append ([])

Another way to write this is
data_out = [[]] * len(data_in)
....if you're willing to put up with this side-effect:
>>data_in = range(10)
data_out = [[]] * len(data_in)
data_out
[[], [], [], [], [], [], [], [], [], []]
>>data_out[0].append('hello' )
data_out
[['hello'], ['hello'], ['hello'], ['hello'], ['hello'],
['hello'], ['hello'], ['hello'], ['hello'], ['hello']]

For less flakey results:
>>data_out = [[] for _ in data_in]
data_out
[[], [], [], [], [], [], [], [], [], []]
>>data_out[0].append('hello' )
data_out
[['hello'], [], [], [], [], [], [], [], [], []]
-tkc
Jan 11 '08 #36

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

Similar topics

3
7387
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is declared in the loop header, but this time that isn't the case: I've got a piece of code a bit like this: #include <iostream> #include <cmath>
12
2094
by: Danny Colligan | last post by:
In the following code snippet, I attempt to assign 10 to every index in the list a and fail because when I try to assign number to 10, number is a deep copy of the ith index (is this statement correct?). .... number = 10 .... So, I have to resort to using enumerate to assign to the list:
6
13770
by: Bora Ji | last post by:
Please help me to creating dynamic VARIABLE in java, with predefined name.
3
2812
by: dowlingm815 | last post by:
I am receiving a Do While Syntax Error - Loop without Do - Can't See it. I would appreciate any fresh eyes? Mary Private Sub Create_tblNJDOC() On Error GoTo Err_Hndlr
0
9665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.