Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27055

Re: Strange behavior

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Strange behavior
Date 2012-08-14 15:05 -0400
References <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> <87lihhpiq9.fsf@dpt-info.u-strasbg.fr>
Newsgroups comp.lang.python
Message-ID <mailman.3284.1344971165.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 8/14/2012 11:59 AM, Alain Ketterlin wrote:
> light1quark@gmail.com writes:
>
>> However if you run the code you will notice only one of the strings
>> beginning with 'x' is removed from the startingList.
>
>>
>> def testFunc(startingList):
>> 	xOnlyList = [];
>> 	for str in startingList:
>> 		if (str[0] == 'x'):
>> 			print str;
>> 			xOnlyList.append(str)
>> 			startingList.remove(str) #this seems to be the problem
>> 	print xOnlyList;
>> 	print startingList
>> testFunc(['xasd', 'xjkl', 'sefwr', 'dfsews'])
>>
>> #Thanks for your help!
>
> Try with ['xasd', 'sefwr', 'xjkl', 'dfsews'] and you'll understand what
> happens. Also, have a look at:
>
> http://docs.python.org/reference/compound_stmts.html#the-for-statement
>
> You can't modify the list you're iterating on,

Except he obviously did ;-).
(Modifying set or dict raises SomeError.)

Indeed, people routine *replace* items while iterating.

def squarelist(lis):
     for i, n in enumerate(lis):
         lis[i] = n*n
     return lis

print(squarelist([0,1,2,3,4,5]))
# [0, 1, 4, 9, 16, 25]

Removals can be handled by iterating in reverse. This works even with 
duplicates because if the item removed is not the one tested, the one 
tested gets retested.

def removeodd(lis):
     for n in reversed(lis):
         if n % 2:
             lis.remove(n)
         print(n, lis)

ll = [0,1, 5, 5, 4, 5]
removeodd(ll)
 >>>
5 [0, 1, 5, 4, 5]
5 [0, 1, 4, 5]
5 [0, 1, 4]
4 [0, 1, 4]
1 [0, 4]
0 [0, 4]

> better use another list to collect the result.

If there are very many removals, a new list will be faster, even if one 
needs to copy the new list back into the original, as k removals from 
len n list is O(k*n) versus O(n) for new list and copy.

> P/S: str is a builtin, you'd better avoid assigning to it.

Agreed. People have actually posted code doing something like

...
list = [1,2,3]
...
z = list(x)
...
and wondered and asked why it does not work.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Strange behavior light1quark@gmail.com - 2012-08-14 08:38 -0700
  Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-14 17:59 +0200
    Re: Strange behavior Terry Reedy <tjreedy@udel.edu> - 2012-08-14 15:05 -0400
  Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-14 21:40 +0200
    Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-15 00:19 +0000
      Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-16 13:18 +0200
        Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:40 +0000
      Re: Strange behavior Peter Otten <__peter__@web.de> - 2012-08-16 15:02 +0200
  Re: Strange behavior light1quark@gmail.com - 2012-08-14 12:20 -0700
    Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:57 +0200
  Re: Strange behavior Chris Angelico <rosuav@gmail.com> - 2012-08-15 07:55 +1000
    Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:50 +0200

csiph-web