Python Forum
Making a copy list in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making a copy list in a function
#1
Question 
I'm getting a error in the function (make_great). I saw that we use [:] to pass a copy of the list to a function, but it's not working.
Does someone understand why this problem is occurring ?

def show_magicians(magicians_list):
    for indice, magician in enumerate(magicians_list):
        print(f'Magician {indice+1}: {magician}')

def make_great(magicians_list[:]):
    list_aux = []
    for magician in magicians_list:
        magician = 'O grande ' + magician
        list_aux.append(magician)
    return list_aux

magicians_aux = make_great(magicians)
show_magicians(magicians)
show_magicians(magicians_aux)
Yoriz write Jul-11-2021, 02:02 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
def make_great(magicians_list[:]):
Don't use [:] on the parameter

Use [:] inside the function
def make_great(magicians_list):
    list_aux = []
    for magician in magicians_list[:]:
        ...
        ...

If you actually want to pass in a copy of the list and the function uses the passed-in list, use
magicians_aux = make_great(magicians[:])

def make_great(magicians_list):
    list_aux = []
    for magician in magicians_list:
        ...
        ...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 269 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy List Not Copying BAbdulBaki 3 624 Aug-19-2023, 02:03 AM
Last Post: perfringo
  Making a function more efficient CatorCanulis 9 1,835 Oct-06-2022, 07:47 AM
Last Post: DPaul
  About list copy. water 3 1,553 Apr-03-2022, 02:42 AM
Last Post: deanhystad
  Making a code.py file to function, does not run hobbyist 6 2,915 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  making list in looping a dictionary glennford49 9 3,569 Jun-25-2020, 03:23 PM
Last Post: ndc85430
  making a function that writes variables (is possible?) miker2808 3 2,350 Jan-30-2020, 06:27 PM
Last Post: buran
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,307 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Making a generalised CSV COPY script in Python Sandy7771989 3 2,431 Jul-05-2019, 11:02 PM
Last Post: Larz60+
  making the code easier, list comprehension go127a 2 2,065 May-26-2019, 06:19 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020