Python Forum
Sorting a copied list is also sorting the original list ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting a copied list is also sorting the original list ?
#1
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)):
        temp=t[k]
        j=k
        while j>0 and temp<t[j-1]:
            t[j]=t[j-1]
            j-=1
            t[j]=temp
    return t

C1=[7,3,6,5,4,2,1]
C2=C1
tri_ins(C2)
I know it's possible using the "sort" function but unfortunately I'm not allow to use it. Sad

C1=[7,3,6,5,4,2,1]
C2=C1
C2=sorted(C1)
Thank you for your help.
Reply


Messages In This Thread
Sorting a copied list is also sorting the original list ? - by SN_YAZER - Apr-11-2019, 04:49 PM
RE: Sort list - by ichabod801 - Apr-11-2019, 05:03 PM
RE: Sort list - by Yoriz - Apr-11-2019, 05:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 462 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
Thumbs Up Sorting Steps MoreMoney 19 1,214 Mar-31-2024, 10:59 AM
Last Post: Pedroski55
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,374 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Variable sorting methods for Enum DataClasses koen 1 861 May-30-2023, 07:31 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,745 May-01-2023, 09:06 PM
Last Post: deanhystad
  Need help with sorting lists as a beginner Realist1c 1 828 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,008 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Check DataFrames with different sorting in columns and rows Foxyskippy 0 833 Nov-19-2022, 07:49 AM
Last Post: Foxyskippy
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 2,018 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  List Sorting Problem ZZTurn 5 1,454 Sep-22-2022, 11:23 PM
Last Post: ZZTurn

Forum Jump:

User Panel Messages

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