Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Sorting a copied list is also sorting the original list ?
Hello, Big Grin
I would need your help to sort the copy of a list without modifying the initial list. Huh

for example, when I create a copy of the C1 list. Then I sort this copy. Both lists are unfortunately sorted. Undecided

def tri_ins(t):
    for k in range(1,len(t)):
        while j>0 and temp<t[j-1]:
    return t

I know it's possible using the "sort" function but unfortunately I'm not allow to use it. Sad

Thank you for your help.
The problem is that you are not creating a copy. Lists are mutable, and as a consequence list variables are actually pointers to the lists rather than the lists themselves. So C2 = C1 just makes C2 point to the same list C1 points to. The easiest way to copy a list is with a full list slice (C2 = C1[:]. Note that this is a "shallow" copy. Any lists within the copied lists will still be references to their original list.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

This is just making C2 also point at the same object as C1

To make a new copy of the list use
c2 = c1[:]
c2 = list(c1)
oooooh ok... Dance
Thank you very much to both of you! Heart

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  create a list of object with a list of character studenthch 0 70 Feb-12-2020, 08:43 AM
Last Post: studenthch
  sorting list arian29 2 114 Feb-02-2020, 10:31 AM
Last Post: ndc85430
  Sorting Arrays in Ascending vino689 8 265 Jan-12-2020, 07:42 AM
Last Post: vino689
  how to compare a list to a list of lists kevthew 1 149 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 609 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 141 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  sorting out the multitude of installation errors ajetrumpet 5 207 Dec-06-2019, 01:19 AM
Last Post: Larz60+
  item = index position - list of list RavCOder 9 295 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  List Comprehension - Creating a list of the length of an item help paul41 2 236 Nov-18-2019, 10:21 AM
Last Post: perfringo
  Sorting columns from text file help nickglobal101 1 212 Nov-15-2019, 07:05 PM
Last Post: newbieAuggie2019

Forum Jump:

Users browsing this thread: 1 Guest(s)