beginner question about range with list and list

2006-11-23 Thread erik gartz
Hello,
I'm new to python and I'm having difficulty understanding the following
code. Why doesn't the variable a contain  [[{}, {'x': 0}, {}], [{},
{'x': 1}, {}]] instead. Doesn't {} allocate new memory for the
dictionary each time? It almost appears as if the 2nd dictionary
created overwrites the first one. Thanks for your help,
Erik

>>>
>>> a = [[{}] *  3] * 2
>>> a
[[{}, {}, {}], [{}, {}, {}]]
>>> for i in range(2):
a[i][1] = {'x':i}
>>> a
[[{}, {'x': 1}, {}], [{}, {'x': 1}, {}]]
>>>

-- 
http://mail.python.org/mailman/listinfo/python-list


for loop without variable

2008-01-09 Thread erik gartz
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: for loop without variable

2008-01-09 Thread erik gartz
On Jan 9, 8:35 pm, Dan Sommers <[EMAIL PROTECTED]> wrote:
> On Wed, 09 Jan 2008 14:25:36 -0800, erik gartz wrote:
> > 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 ...
>
> What does that loop do?  (Not the loop you posted, but the "real" loop
> in your application.)  Perhaps python has another way to express what
> you're doing.
>
> For example, if you're iterating over the elements of an array, or
> through the lines of a file, or the keys of a dictionary, the "in"
> operator may work better:
>
> for thing in array_or_file_or_dictionary:
> do_something_with(thing)
>
> HTH,
> Dan
>
> --
> Dan Sommers   A death spiral goes clock-
> <http://www.tombstonezero.net/dan/>   wise north of the equator.
> Atoms are not things. -- Werner Heisenberg  -- Dilbert's PHB

The loop performs some actions with web services. The particular
iteration I'm on isn't important to me. It is only important that I
attempt the web services that number of times. If I succeed I
obviously break out of the loop and the containing function (the
function which has the loop in it) returns True. If all attempts fail
the containing loop returns False.

I guess based on the replies of everyone my best bet is to leave the
code the way it is and suck up the warning from pylint. I don't want
to turn the warning off because catching unused variables in the
general is useful to me. Unfortunately, I don't *think* I can shut the
warning for that line or function off, only for the entire file.
Pylint gives you a rating of your quality of code which I think is
really cool. This is a great motivation and helps me to push to
"tighten the screws". However it is easy to get carried away with your
rating.:-)
-- 
http://mail.python.org/mailman/listinfo/python-list