Python - Sort/Alphabetize List Using Sort() and Sorted() Methods

by matt392 in Circuits > Software

23 Views, 1 Favorites, 0 Comments

Python - Sort/Alphabetize List Using Sort() and Sorted() Methods

sort-method.png
# Created using Thonny IDE for Python

print ("Create a list called 'linuxlist'. ")
linuxlist = ["Puppy", "Debian", "Fedora", "Manjaro", "Gentoo", "Kali", "Slackware", "Ubuntu", "MxLinux", "OpenSuse"]
print ("The first list is: ", linuxlist)
print ("Sort the list using sort() method.")
linuxlist.sort()
# Please note: This list method is void and return "None"
# sortedlist = linuxlist.sort()
# sortedlist will contain "None" and *not* the sorted list
print ("Linuxlist sorted: ", linuxlist)

print ("=============")

print ("Create a list called 'linuxlist2'. ")
linuxlist2 = ["Puppy", "Debian", "Fedora", "Manjaro", "Gentoo", "Kali", "Slackware", "Ubuntu", "MxLinux", "OpenSuse"]
print ("The 2nd list is:", linuxlist2)
print ("Sort the list using the sorted() method and place into variable")
# Sort the list and place into variable
sortedlinuxlist2 = sorted(linuxlist2)
print ("Linuxlist2 sorted with sorted():", sortedlinuxlist2)

Downloads