xprior
xprior

Reputation: 1

How can I change one list without changing the other?

This is my code:

 new_final_array=[x for x in new_array]
    for a in range(len(array)):
        for d in range(2):
            for l in range(len(new_array)):
                if new_array[l][d]==array[a][1]:
                    print l,d
                    new_final_array[l][d]=array[a][0]

I created list1(new_final_array) based on list2(new_array) and if I change one element on list1 it will also change on list2. How can I make them independent?

Upvotes: 0

Views: 96

Answers (2)

pawelswiecki
pawelswiecki

Reputation: 572

I'm not sure if I understand, but maybe copy.deepcopy will be of some use.

import copy
new_list = copy.deepcopy(old_list)

See documentation.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

Copy one level deeper.

new_final_array=[x[:] for x in new_array]

Upvotes: 3

Related Questions