Making a copy list in a function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Making a copy list in a function (/thread-34252.html) |
Making a copy list in a function - RuyCab - Jul-11-2021 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) RE: Making a copy list in a function - Yoriz - Jul-11-2021 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: ... ... |