0

no = ['hello','world','spam','eggs']

yes = ['hey','spaam']

How can I do something like yes=no? I want the list "yes" to be exactly like the list "no".

Roy
  • 31
  • 3

1 Answers1

3

You can do it by:

no = ['hello','world','spam','eggs']
yes = ['hey','spaam']

yes.clear()
yes.extend(no)

This way, instance of yes will be persevere and extended with the content of no

majkrzak
  • 1,332
  • 3
  • 14
  • 30
  • The duplicated answer is already full of detail and precision, don't spread the information, if you keep your answer, the next people won't take a look at the detailed one, if you remove yours, they will – azro Dec 15 '19 at 09:37
  • Yea, sorry. I have not whole SO indexed in my mind. I was a bit surprised with this question, but now i see we were wrong :D – majkrzak Dec 15 '19 at 09:39
  • I don't blame you, just telling, experience is the only way to know the SO question ;) – azro Dec 15 '19 at 09:40
  • WIll be nice to repoen the quest, cause ehe asked how to copy one list INTO another @azro – majkrzak Dec 15 '19 at 09:41
  • 1
    what's the difference ? It's just have a new list that exactly he content of the first one, he did not meant INTO like addAll – azro Dec 15 '19 at 10:14
  • @azro diference is huge here. He don't ask about NEW list, he wanted `yes` to have content of `no`. Creating new list is not a solution – majkrzak Dec 15 '19 at 13:17
  • it’s just the same ... it you want to have the same values but not same reference you create a new list, it you want 2 values point the same list : yes=no – azro Dec 15 '19 at 13:18
  • the point is he already have 2 lists, `yes` and `no` he don't want a third one – majkrzak Dec 15 '19 at 13:20
  • There is no gain at 'not create a new one', you'll loose to empty the second and then refill it^^ For ex it you want to empty a list you'll just do : mylist = [] (so create a new one) , a new list IS NOT a new variable ^^ – azro Dec 15 '19 at 17:29
  • ```>>> id(no) 140348859871168 >>> id(yes) 140348859871040 >>> yes=no.copy() >>> id(yes) 140348859629120``` sure – majkrzak Dec 15 '19 at 18:22
  • @azor it is now entity, or how ever you call it in python – majkrzak Dec 15 '19 at 18:23