Python Forum
sort a list alphabeticaly without changing the original list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sort a list alphabeticaly without changing the original list
#1
I'm supposed to reverse a list of countries I want to visit (alphabetical)

list = ['Poland', 'Russia', 'Germany', 'USA', 'China']
How can I do this without changing the list permanently?
Reply
#2
first of all dont save it to built in list

lister[::-1]
Recommended Tutorials:
Reply
#3
please, use meaningful titles, this time I changed it for you.
don't use list as variable name. it's a built-in function and you override it
>>> countries = ['Poland', 'Russia', 'Germany', 'USA', 'China']
>>> sorted(countries)
['China', 'Germany', 'Poland', 'Russia', 'USA']
>>> countries
['Poland', 'Russia', 'Germany', 'USA', 'China']
>>> 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jan-27-2019, 01:23 PM)buran Wrote: please, use meaningful titles, this time I changed it for you. don't use list as variable name. it's a built-in function and you override it
>>> countries = ['Poland', 'Russia', 'Germany', 'USA', 'China'] >>> sorted(countries) ['China', 'Germany', 'Poland', 'Russia', 'USA'] >>> countries ['Poland', 'Russia', 'Germany', 'USA', 'China'] >>> 

the task from the Python book says

"Use sorted () to print your list in reverse alphabetical order without changing the order of the original list"

"Show that your list is still in its original order by printing it again"
Reply
#5
>>> sorted(countries, reverse=True)
['USA', 'Russia', 'Poland', 'Germany', 'China']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
When I try printing again, I don't get it in the regular order. How do I revert from reverse alphabetical to stock?

E.g:

sorted(countries, reverse=True)
['USA', 'Russia', 'Poland', 'Germany', 'China']
now... I get the reverse alphabetical order... Now, how do I get out of this variable and print the stock list again?

sorted(countries, reverse=True)
print (countries)

print ("\nHere is the original list:")
### I want the original here ###

locations = ['himalaya', 'andes', 'tierra del fuego', 'labrador', 'guam']

print("Original order:")
print(locations)

print("\nAlphabetical:")
print(sorted(locations))

print("\nOriginal order:")
print(locations)

print("\nReverse alphabetical:")
print(sorted(locations, reverse=True))

print("\nOriginal order:")
print(locations)

print("\nReversed:")
locations.reverse()
print(locations)

print("\nOriginal order:")
locations.reverse()
print(locations)

print("\nAlphabetical")
locations.sort()
print(locations)

print("\nReverse alphabetical")
locations.sort(reverse=True)
print(locations)
found out my book had a support site...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,438 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  how to reverse a list and store in another list in python SuperNinja3I3 6 3,267 Aug-14-2022, 06:36 PM
Last Post: DeaD_EyE
Question Python - List - Int List sophi 8 2,519 Apr-21-2022, 07:55 PM
Last Post: sophi
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,339 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,401 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Check if a list exists in given list of lists Daniel94 2 2,227 Apr-07-2020, 04:54 PM
Last Post: deanhystad
  list of strings to list of float undoredo 3 2,668 Feb-19-2020, 08:51 AM
Last Post: undoredo
  arrays sum list unsupported operand type(s) for +=: 'int' and 'list' DariusCG 7 4,142 Oct-20-2019, 06:24 PM
Last Post: Larz60+
  have homework to add a list to a list using append. celtickodiak 2 1,991 Oct-11-2019, 12:35 PM
Last Post: ibreeden
  Search character from 2d list to 2d list AHK2019 3 2,461 Sep-25-2019, 08:14 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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