Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ONE input => THREE outputs
#5
For mutable objects, copy.copy and/or copy.deepcopy can be used.
Lists, Dicts, Sets have a copy method.
Lists can also copied with:

import copy


my_list_1 = [1, 2, 3]
my_list_2 = my_list_1[:]
my_list_3 = my_list_1.copy()
my_list_4 = copy.copy(my_list_1) 
Lists can keep also lists and other objects.
To copy them together with the nested content, copy.deepcopy should be used.

import copy


nested_list_1 = [1, 2, [3, 4, 5], 6]
nested_list_2 = nested_list_1[:] # the inner list is not copied
nested_list_3 = copy.deepcopy(nested_list_1) # inner list is also copied

nested_nested_list_1 = [[[1]], [[2]]]
nested_nested_list_2 = copy.deepcopy(nested_nested_list_1)

# now changing the 1 to 66
nested_nested_list_2[0][0][0] = 66

# nested_nested_list_1 has the original value 1
# nested_nested_list_2 has instead 66
print(nested_nested_list_1)
print(nested_nested_list_2)
Docs for copy module: https://docs.python.org/3/library/copy.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
ONE input => THREE outputs - by Tricia279 - Jan-12-2021, 01:05 PM
RE: ONE input => THREE outputs - by DeaD_EyE - Jan-12-2021, 01:11 PM
RE: ONE input => THREE outputs - by RubenF85 - Jan-12-2021, 03:25 PM
RE: ONE input => THREE outputs - by Tricia279 - Jan-14-2021, 07:42 AM
RE: ONE input => THREE outputs - by perfringo - Jan-14-2021, 08:52 AM
RE: ONE input => THREE outputs - by buran - Jan-12-2021, 01:45 PM
RE: ONE input => THREE outputs - by DeaD_EyE - Jan-12-2021, 07:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  format json outputs ! evilcode1 3 1,763 Oct-29-2023, 01:30 PM
Last Post: omemoe277
  Formatting outputs created with .join command klairel 2 640 Aug-23-2023, 08:52 AM
Last Post: perfringo
  I have written a program that outputs data based on GPS signal kalle 1 1,187 Jul-22-2022, 12:10 AM
Last Post: mcmxl22
  Why does absence of print command outputs quotes in function? Mark17 2 1,395 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  Thoughts on interfacing with a QR code reader that outputs keystrokes? wrybread 1 1,493 Oct-08-2021, 03:44 PM
Last Post: bowlofred
  Combining outputs into a dataframe rybina 0 1,691 Mar-15-2021, 02:43 PM
Last Post: rybina
  Multi set string inputs/outputs kwmcgreal 2 2,090 Sep-26-2020, 10:44 PM
Last Post: kwmcgreal
  How to use subprocess to get multiple data outputs in desired folder? 3SG14 1 2,236 Sep-19-2020, 05:46 PM
Last Post: bowlofred
  Outputs missing SamAnw 4 2,654 Feb-12-2020, 04:32 PM
Last Post: adetheheat
  Interpreter and running a .py file give different outputs PythonNPC 5 3,046 Jul-21-2019, 01:07 PM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

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