Python - Function that copies content from one list to another list

Asked

Viewed 158 times

0

I need a function (copie_list, transcribed below) that takes two lists, dest and orig, and copies the content of orig to dest, see example below:

>>> a = [8, 9]
>>> b = [1, 2, 3, 4]
>>> copia_lista(a, b)
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]

The code I have implemented below does not work. Would anyone like to explain why?

def copia_lista(dest, orig):
    ''' (list, list) -> None'''

    dest = orig.copy()

5 answers

6

I don’t see it as something useful for production, but supposing it’s just an exercise, let’s go.

def copia_lista(dest, orig):
    ''' (list, list) -> None'''
    dest = orig.copy()

When you do this you’re basically creating an object dest new, in the local scope of the function, which in this scope will overwrite the object dest received as parameter. When the function finishes executing, the local scope is terminated and the object dest that you created is lost, keeping unchanged the dest original.

As list is a changeable type, just change the object without making a new assignment.

def copia_lista(dest, orig):
    ''' (list, list) -> None'''
    dest.clear()  # Remove os elementos de dest
    for item in orig:
        dest.append(item)

In this way,

>>> a = [8, 9]
>>> b = [1, 2, 3, 4]
>>> copia_lista(a, b)
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]

A simpler way to execute the same logic would be:

def copia_lista(dest, orig):
    ''' (list, list) -> None'''
    dest[:] = orig

Producing the same result.

2

According to that answer, the moment you do:

dest =orig.copy()

What the interpreter does is take the name (or "label") dest, of the internal scope, and place it on the object returned by the method orig.copy(). The name a of the call of the continuous function coupled to the same original object.

One way to do this would be to modify its function to have a return, and that return be assigned to the desired variable:

def copia(orig):
    return orig.copy()
a=copia(b)

1

There is no (appropriate) way to modify Python objects using a function. You cannot (or should not) change the value of b within the function copy list.

The appropriate way is to assign the return of a function to the variable:

def copia_lista(orig):
  return orig.copy()

a = [8, 9]
b = [1, 2, 3, 4]
a = copia_lista(b)
print(a)
print(b)

Upshot:

[1, 2, 3, 4]
[1, 2, 3, 4]

That said, if by "copying" you meant that what you wanted was to add the content of b in to, then the method is another:

def copia_lista(dest, orig):
  return dest + orig

a = [8, 9]
b = [1, 2, 3, 4]
a = copia_lista(a, b)
print(a)
print(b)

Upshot:

[8, 9, 1, 2, 3, 4]
[1, 2, 3, 4]

And for those who did not understand the usefulness of this code, within the function copy list we can introduce other processing that simply by doing a = b.copy() doesn’t make.

1

I didn’t understand the usefulness of the code, but in a direct way it would look like this:

import copy


def copia_lista(lista):
    '''Retorna a cópia de uma lista. '''
    return copy.copy(lista)

lista_A = [1, 2, 3]

copia = copia_lista(lista_A)    

lista_A.append(9)

print(copia) # [1, 2, 3]
print(lista_A) # [1, 2, 3, 9]

From this furnace you have two lists allocated in different parts in memory can manipulate one and keep the original intact. I hope to have helped.

  • This is not exactly the solution previously presented in this answer?

0

There is another way less efficient, but it does the same thing and the more methods better, since q can be applied in other situations.

def Copiar(B):
    for i in B:
        A.append(i)


A=[1,2,3,4]
B=[5,6,7,8]

Copiar(B)

print(A)
print(B)

There’s the attribution no return was what you were trying to do.

  • 1

    The only detail is that it keeps the initial values of the list while it would be expected, given the question, that they be removed.

  • That. but I think that’s what he was trying to do, since he passed two lists and was dissatisfied by the return Equal... the logic that I think he kind of went... with the def copy(a,b) type copy b is together with the n know.. I just assume, regardless of that now it contains more acquired knowledge :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.