[Tutor] Unintentionally manipulating a list

Alex Hall mehgcap at gmail.com
Fri Feb 25 15:52:56 CET 2011


You may want to deepcopy instead of just copy?

On 2/25/11, ranjan das <ranjand2005 at gmail.com> wrote:
> I am facing the following problem
>
>
> I have a list of the form
>
> INPUT= [ [ ['A1-N','A2-Y','A3-N' ],['B1-Y','B2-N','B3-N' ] ], [.........] ]
>
>
> and I want an output of the form (selecting only those elements which have a
> Y associated with them)
>
> OUTPUT=[ ['A2-Y', 'B1-Y'],[....] ]
>
>
> I wrote the following code. Although it gives me the desired output, it
> CHANGES the list INPUT
>
> now after i run the code I get INPUT as the same as OUTPUT (which i dont
> want to happen). I have used the copy function but it still is not working.
> Any help or pointers is appreciated
>
> *CODE*
>
> from copy import copy
>
> temp=copy( INPUT )
> OUTPUT=temp
>
>
>
> for i in range(len(temp)):
>
>     for j in range(len(temp[i])):
>
>         for k in range(len(temp[i][j])):
>
>             if temp[i][j][k][-1]=='Y':
>
>                 OUTPUT[i][j]=temp[i][j][k]
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list