I am trying to sort the list of ipaddress from the following list.
IPlist= ['209.85.238.4', '216.239.51.98', '64.233.173.198', '64.3.17.208', '64.233.173.238']
#1st case
tmp1 = [list (map (str, ip.split("."))) for ip in IPlist]tmp1.sort()
print(tmp1)
When I run this snippet. I got the following output.
[['209', '85', '238', '4'], ['216', '239', '51', '98'], ['64', '233', '173', '198'], ['64', '233', '173', '238'], ['64', '3', '17', '208']]
#second case
tmp = [tuple (map (int, ip.split("."))) for ip in IPlist]
# print(tmp)tmp.sort ()
print(tmp)
When I run the second case,I got the following output.
[(64, 3, 17, 208), (64, 233, 173, 198), (64, 233, 173, 238), (209, 85, 238, 4), (216, 239, 51, 98)]
The only thing that I found the difference is the conversion to either string or int function using above function. But even if the intial values are string, doesnt sort() function works in the same way?
For eg:
lst = ['23', '33', '11', '7', '55']# Using sort() function with key as int
lst.sort(key = int)print(lst)
Output: ['7', '11', '23', '33', '55']