1

I am trying to make a duplicate list of lists and change one element to another within the nested lists of the duplicate list but am having some trouble. How I made the duplicate list:

order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 'gap', ['yhjK', 'R']]
#order_1 = list(order) #this makes the duplicate list as well
order_1 = []
for x in order:
    order_1.append(x)

How I changed the elements:

for item in order_1:
    for n,i in enumerate(item):
            if i=='R':
                    item[n]='F'
            if i=='F':
                    item[n]='R'

I want to replace all the 'F' with 'R' and vice versa. This accomplishes that but the original list 'order' is changed as well. I only want the second list to be changed and can't figure out what is the problem with my code.

What I get:

order = [['yhjK', 'R'], 'gap', ['bcsA', 'R'], ['bcsB', 'R'], ['bcsZ', 'R'], 'gap', ['yhjK', 'F']]
order_1 = [['yhjK', 'R'], 'gap', ['bcsA', 'R'], ['bcsB', 'R'], ['bcsZ', 'R'], 'gap', ['yhjK', 'F']]

What I want:

order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 'gap', ['yhjK', 'R']]
order_1 = [['yhjK', 'R'], 'gap', ['bcsA', 'R'], ['bcsB', 'R'], ['bcsZ', 'R'], 'gap', ['yhjK', 'F']]

Thanks everyone!

5 Answers 5

4

What you're doing here is a shallow copy of the list, so when you change the copy, the original changes as well. What you need is deepcopy

import copy
order = [['yhjK', 'F'], 'gap', ['bcsA', 'F'], ['bcsB', 'F'], ['bcsZ', 'F'], 
'gap', ['yhjK', 'R']]
order_1 = copy.deepcopy(order)

# Now changing order_1 will not change order
order_1[1] = ['TEST LIST']
print order[1] # Prints 'gap'
print order_1[1] # Prints '['TEST LIST']
Sign up to request clarification or add additional context in comments.

Comments

3
L = [['F' if x == 'R' else 'R' if x == 'F' else x for x in row] for row in order]

4 Comments

+1000; don't make a copy and then change it, just make the thing you want to make.
@KarlKnechtel, yes, but a little hard for a beginner to understand.
The only issue is that the 'gap' elements become ['g','a','p'] in the output. I don't know how to fix that... @KarlKnechtel I didn't think of the problem that way. But yes, that probably makes things simpler. Dhara, I am very much a beginner and was/am a bit confused with this.
@Elisabeth: to fix 'gap' you could [[...] if isinstance(row, list) else row for row in order] though it stratches the limits of list comprehension. btw, nested list comprehension is described in the tutorial that every beginner should read.
1
import copy
order_1 = copy.deepcopy(order)

Python by default only copies references to mutable values, so changing them in one place results in them being changed everywhere. Creating a deep copy means the two instances are completely independent.

Comments

0
order_1 = []
for item in order:
    temp = []
    for i in item:
        if i=="F":
            temp.append("R")
        elif i=="R"
            temp.append("F")
        else:
            temp.append(i)
    order_1.append(temp)

Comments

-1

It seems that, as with many other languages, arrays are no more than constants (not even actual pointers although that might be different in python). Rather, The address of the first element of the array is constant. This means that if you would directly copy the array, you would only copy the address of the first element.

From your question, I understand that you want a deep copy. That means that, you also need to copy the contents of you nested arrays. You can use copy.deepcopy(x) to make a deepcopy of the array.

Check out the copy module for more in-depth information.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.