SoFunction
Updated on 2024-11-21

A python implementation of a forbidden function to modify a list

Sometimes it is necessary to prohibit a function from modifying the list. For example, to perform a modification operation on a fission, it is also necessary to keep the original unprinted design list for the record. To solve this problem, a copy of the list can be passed to the function instead of the original; so that any modifications made by the function affect only the copy and not the original in any way.

function_name(list_name[:])

8-9 Magician. Magician:Create a list of magicians' names and pass it to a function called show_magicians(), which prints the name of each magician in the list. 8-10 The Amazing Magicians The Amazing Magicians : After you have completed the exercise for the

8-9 Write a function called make_great() that modifies the list of magicians by adding the words "the Great" to each magician's name. Call the function show_magicians() to confirm that the list of magicians has indeed changed.

8-11 The Immutable Magician The Immutable Magician :Modify the program you wrote to complete Exercises 8-10 by passing a copy of the magician's list to the function make_great() when you call it. Since you don't want to modify the original list, return the modified list and store it in another list. Call show_magicians() with the two lists separately, making sure that one list contains the original magician's name and the other list contains the magician's name with the word "the Great" added.

def make_great(magicians,new_magicians): # Functions that modify the list
 while magicians:
   current_magician = () # Delete elements from the original list
   current_magician = "The Great " + current_magician
   new_magicians.append(current_magician)

def show_magicians(new_magicians):
 for magician in new_magicians:
  # Facilitate all elements in magicians
  print(magician) 

magicians = ['fake','ppd','moon']
new_magicians = []

make_great(magicians[:],new_magicians)# Call function make_great Pass magicians[] vice magicians[:]
show_magicians(new_magicians)# Output new table
show_magicians(magicians)#Input original form

Implementation results:

The Great moon
The Great ppd
The Great fake
fake
ppd
moon

Above this python prohibit function to modify the list of the implementation method is all I have shared with you, I hope to give you a reference, and I hope you support me more.