Repository navigation
Expand file tree
/
Copy pathlistPart8.py
More file actions
48 lines (40 loc) · 1.46 KB
/
Copy pathlistPart8.py
File metadata and controls
48 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#Sort list
thislist = [100,50,99,2,56]
thislist.sort()
print(thislist)
# o/p- [2, 50, 56, 99, 100]
# List objects have a sort() method that will sort the list alphanumerically, ascending, by default:
# sort the list alphabetically
thislist = ["ram", "sachin", "angell", "krips" ]
thislist.sort()
print(thislist)
# o/p- ['angell', 'krips', 'ram', 'sachin']
# sort the list numerically
thislist = [100,50,99,2,56]
thislist.sort()
print(thislist)
# o/p- [2, 50, 56, 99, 100]
# Sort Descending
# To sort descending, use the keyword argument reverse = True:
thislist = ["orange","banana","apple","cherry"]
thislist.sort(reverse= True)
print(thislist)
# ['orange', 'cherry', 'banana', 'apple']
# Case Insensitive Sort
# By default the sort() method is case sensitive, resulting in all capital letters being sorted before lower case letters:
thislist = ["orange","Banana","apple","Cherry"]
thislist.sort()
print(thislist)
#o/p- ['Banana', 'Cherry', 'apple', 'orange']
# Luckily we can use built-in functions as key functions when sorting a list.
# So if you want a case-insensitive sort function, use str.lower as a key function:
thislist = ["orange","Banana","apple","Cherry"]
thislist.sort(key= str.lower)
print(thislist)
# o/p- ['apple', 'Banana', 'Cherry', 'orange']
# Reverse order
# What if you want to reverse the order of a list, regardless of the alphabet?
thislist = ["orange","banana","apple","cherry"]
thislist.reverse()
print(thislist)
#o/p- ['cherry', 'apple', 'banana', 'orange']