Discord
Login
Community
DARK THEME

Cloning a list

simple question, I wish to clone a list into another variable, is there a function for this or do I have to iterate through the list myself?

I'm on the phone and can't check, but have a look through existing projects. Yes, you create a 'copy' loop and best is to add it to a helper library. You can extend the List prototype if you like.

ok, so as I understand there is no pre-existing way to do it, I have to write it myself, thanks

  list1 = [1,2,3,4,5,6,7,8,9]
  list2 = ['a','b','c']
  list3 = list2.concat( list1 )
  print(' list1 = '+ list1 )
  print(' list2 = '+ list2 )
  print(' list3 = '+ list3 )
  list4 = list1.concat()
  print(' clone list1 to list4 = '+ list4 )

What a cool hack to use concat as copy replacement @Loginus =)

Never thought of it, LOL
Big thank you for this insight

Live Long and Tinker

If you want to make another list with the same data, all you need to do is

list_copy = list

If you are trying to save a string with comma seperated values, you can do

list_copy = "" + list

Just be careful when you do

list_copy = list

It does not create a copy of list it links them together.
If you modify list_copy it will also modify the content of list.
Basically it copies the internal pointer to the list data, not the list data itself.

Live Long and Tinker

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community