Alex Martelli wrote:
> Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote:
>
>> Alex Martelli wrote:
>>
>> > So, *WHAT ON EARTH* could possibly
>> > make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more
>> > obvious that the second one returns an independent, separate object
>> > initially
Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote:
> Alex Martelli wrote:
>
> > So, *WHAT ON EARTH* could possibly
> > make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more
> > obvious that the second one returns an independent, separate object
> > initially equal to x
>
> .>> x = 2
> .>
Alex Martelli wrote:
> So, *WHAT ON EARTH* could possibly
> make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more
> obvious that the second one returns an independent, separate object
> initially equal to x
.>> x = 2
.>> y = x*1
.>> x is y
True
.>>
just-kidding-ly yours,
Reinhold
--
Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote:
...
> > Using ips[:] to make a copy on the fly is very idiomatic, but I've never
> > liked it, personally. I see no reason to prefer it to the even shorter
> > and arguably less obscure ips*1, for example.
>
> "Less obscure"? Either you know the `
Alex Martelli wrote:
> Bill Mill <[EMAIL PROTECTED]> wrote:
>...
>> > > You are modifying the list as you iterate over it. Instead, iterate over
>> > > a copy by using:
>> > >
>> > > for ip in ips[:]:
>...
>> Once you know it, it's neat, and I use it sometimes. However, it's a
>> little too
Bill Mill <[EMAIL PROTECTED]> wrote:
...
> > > You are modifying the list as you iterate over it. Instead, iterate over
> > > a copy by using:
> > >
> > > for ip in ips[:]:
...
> Once you know it, it's neat, and I use it sometimes. However, it's a
> little too "magical" for my tastes; I'd rat
Yes, I also this that comprehension is the clearer way to handle this. You
might also consider the good old "filter" function :
ips = filter(lambda ip: '255' not in ip, ips)
Francis Girard
Le vendredi 4 Février 2005 20:38, rbt a écrit :
> Thanks guys... list comprehension it is!
>
> Bill Mill w
rbt wrote:
Alan McIntyre wrote:
I think it's because you're modifying the list as you're iterating
over it.
One last clarification on this. It's OK to modify the elements of a
list, but not the list itself while iterating over it... is that the
correct way to think about this?
Correct - the i
rbt wrote:
John J. Lee wrote:
Steve Holden <[EMAIL PROTECTED]> writes:
[...]
You are modifying the list as you iterate over it. Instead, iterate
over a copy by using:
for ip in ips[:]:
...
Just to help popularise the alternative idiom, which IMO is
significantly less cryptic (sane constructors of
Terry Reedy wrote:
"Steve Holden" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
[ ... ]
This seems much more sensible to me than building a new list with
everything (a copy), including things you don't want, and then deleting the
things you don't want (an O(n**2) operation).
Whic
"Steve Holden" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> You are modifying the list as you iterate over it. Instead, iterate over
> > a copy by using:
> for ip in ips[:]:
Or if you are only deleting items, you can iterate backwards.
You can also build a new list with only
Bill Mill wrote:
On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote:
John J. Lee wrote:
Steve Holden <[EMAIL PROTECTED]> writes:
[...]
You are modifying the list as you iterate over it. Instead, iterate
over a copy by using:
for ip in ips[:]:
...
Just to help popularise the altern
On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote:
> John J. Lee wrote:
> > Steve Holden <[EMAIL PROTECTED]> writes:
> > [...]
> >
> >>You are modifying the list as you iterate over it. Instead, iterate
> >>over a copy by using:
> >>
> >>for ip in ips[:]:
> >> ...
> >
> >
> > Just
On Fri, 04 Feb 2005 14:49:43 -0500, rbt wrote:
> Alan McIntyre wrote:
>> I think it's because you're modifying the list as you're iterating over
>
> In this case then, shouldn't my 'except Exception' raise an error or
> warning like:
>
> "Hey, stupid, you can't iterate and object and change it
John J. Lee wrote:
Steve Holden <[EMAIL PROTECTED]> writes:
[...]
You are modifying the list as you iterate over it. Instead, iterate
over a copy by using:
for ip in ips[:]:
...
Just to help popularise the alternative idiom, which IMO is
significantly less cryptic (sane constructors of mutable o
Steve Holden <[EMAIL PROTECTED]> writes:
[...]
> You are modifying the list as you iterate over it. Instead, iterate
> over a copy by using:
>
> for ip in ips[:]:
>...
Just to help popularise the alternative idiom, which IMO is
significantly less cryptic (sane constructors of mutable objects
Alan McIntyre wrote:
I think it's because you're modifying the list as you're iterating over
it.
One last clarification on this. It's OK to modify the elements of a
list, but not the list itself while iterating over it... is that the
correct way to think about this?
--
http://mail.python.org/m
On Fri, 04 Feb 2005 14:43:30 -0500, rbt <[EMAIL PROTECTED]> wrote:
> Steve Holden wrote:
> > rbt wrote:
> >
> >> Either I'm crazy and I'm missing the obvious here or there is
> >> something wrong with this code. Element 5 of this list says it doesn't
> >> contain the string 255, when that's *ALL* i
Alan McIntyre wrote:
I think it's because you're modifying the list as you're iterating over
In this case then, shouldn't my 'except Exception' raise an error or
warning like:
"Hey, stupid, you can't iterate and object and change it at the same time!"
Or, perhaps something similar?
--
http://mai
Steve Holden wrote:
You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:
for ip in ips[:]:
...
Also worth noting, you can write this like:
for ip in list(ips):
...
if you're afraid of smiley-faces [:] in your code. ;)
Steve
--
http://mail.python.
"rbt" <[EMAIL PROTECTED]> wrote:
>> You are modifying the list as you iterate over it. Instead, iterate over a
>> copy by using:
>>
>> for ip in ips[:]:
>> ...
>>
>> regards
>> Steve
>
> Very neat. That's a trick that everyone should know about.
I suppose that's why it's included in the Pytho
Steve Holden wrote:
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is
something wrong with this code. Element 5 of this list says it doesn't
contain the string 255, when that's *ALL* it contains... why would it
think that???
import time
ips = ['255.255.255.255', '128.173.
Wow, that's cool; I'd never seen that before. :) Thanks, Steve..
Steve Holden wrote:
You are modifying the list as you iterate over it. Instead, iterate over
a copy by using:
for ip in ips[:]:
...
regards
Steve
--
http://mail.python.org/mailman/listinfo/python-list
Thanks guys... list comprehension it is!
Bill Mill wrote:
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <[EMAIL PROTECTED]> wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL*
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???
import time
ips = ['255.255.255.255', '128.173.120.79', '198.82.247.
rbt wrote:
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???
import time
ips = ['255.255.255.255', '128.173.120.79', '198.82.247.
I think it's because you're modifying the list as you're iterating over
it. Try this:
import time
ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98',
'127.0.0.1', '255.0.0.0', '255', '128.173.255.34']
ips_new = []
for ip in ips:
if '255' not in ip:
ips_new.append(ip)
print
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <[EMAIL PROTECTED]> wrote:
> Either I'm crazy and I'm missing the obvious here or there is something
> wrong with this code. Element 5 of this list says it doesn't contain the
> string 255, when that's *ALL* it contains... why would it think that???
>
> impo
Either I'm crazy and I'm missing the obvious here or there is something
wrong with this code. Element 5 of this list says it doesn't contain the
string 255, when that's *ALL* it contains... why would it think that???
import time
ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98',
'127.
29 matches
Mail list logo