During my Python studies, I came across something that didn’t make much sense to me so I had to learn and investigate (with the help of experts).
What you can usually do in Python is to modify a variable and assign the result to the same variable. Because a piece of code is usually worth much more than an explanation:
1 2 3 4 |
>>> my_variable = "I love networking" >>> my_variable = my_variable + " and Python so much !!" >>> print(my_variable) I love networking and Python so much !! |
When you want to sort a list, that behavior is a bit different:
let’s pretend I have a list of ARP entries into my switch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
['\n', 'Protocol Address Age (min) Hardware Addr Type Interface\n', 'Internet 10.220.88.37 69 0001.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.1 135 0062.ec29.70fe ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.220.88.30 220 5254.ab71.e119 ARPA FastEthernet4\n', 'Internet 10.220.88.32 191 5254.abc7.26aa ARPA FastEthernet4\n', 'Internet 10.220.88.33 190 5254.ab3a.8d26 ARPA FastEthernet4\n', 'Internet 10.220.88.35 175 5254.abfb.af12 ARPA FastEthernet4\n', 'Internet 10.220.88.38 159 0002.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.39 0 6464.9be8.08c8 ARPA FastEthernet4\n', 'Internet 10.220.88.40 144 001c.c4bf.826a ARPA FastEthernet4\n', 'Internet 10.220.88.41 123 001b.7873.5634 ARPA FastEthernet4\n', 'Internet 10.220.88.20 - c89c.1dea.0eb6 ARPA FastEthernet4\n', 'Internet 10.220.88.21 213 1c6a.7aaf.576c ARPA FastEthernet4\n', 'Internet 10.220.88.28 222 5254.aba8.9aea ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.111.11.11 69 abcd.efef.aaaa ARPA FastEthernet1\n'] |
If I want to sort it and reassign the value of it to the previously used variable I would use this code (Let’s pretend arp_entries is my variable that contains all these entries):
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 |
nic@vPackets.local:/Users/nic/github/Python3-Tests git:(master) $ python3 Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pprint import pprint >>> arp_entries = open("show_arp.txt", 'r', encoding = 'utf-8') >>> arp_entries = arp_entries.readlines() >>> pprint(arp_entries) ['\n', 'Protocol Address Age (min) Hardware Addr Type Interface\n', 'Internet 10.220.88.37 69 0001.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.1 135 0062.ec29.70fe ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.220.88.30 220 5254.ab71.e119 ARPA FastEthernet4\n', 'Internet 10.220.88.32 191 5254.abc7.26aa ARPA FastEthernet4\n', 'Internet 10.220.88.33 190 5254.ab3a.8d26 ARPA FastEthernet4\n', 'Internet 10.220.88.35 175 5254.abfb.af12 ARPA FastEthernet4\n', 'Internet 10.220.88.38 159 0002.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.39 0 6464.9be8.08c8 ARPA FastEthernet4\n', 'Internet 10.220.88.40 144 001c.c4bf.826a ARPA FastEthernet4\n', 'Internet 10.220.88.41 123 001b.7873.5634 ARPA FastEthernet4\n', 'Internet 10.220.88.20 - c89c.1dea.0eb6 ARPA FastEthernet4\n', 'Internet 10.220.88.21 213 1c6a.7aaf.576c ARPA FastEthernet4\n', 'Internet 10.220.88.28 222 5254.aba8.9aea ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.111.11.11 69 abcd.efef.aaaa ARPA FastEthernet1\n'] >>> >>> >>> >>> arp_entries = arp_entries.sort() >>> pprint(arp_entries) None |
According to this python official documentation, Python lists have a built-in list.sort() method that modifies the list in place. Let’s verify this:
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 |
nic@vPackets.local:/Users/nic/github/Python3-Tests git:(master) $ python3 Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from pprint import pprint >>> arp_entries = open("show_arp.txt", 'r', encoding = 'utf-8') >>> arp_entries = arp_entries.readlines() >>> pprint(arp_entries) ['\n', 'Protocol Address Age (min) Hardware Addr Type Interface\n', 'Internet 10.220.88.37 69 0001.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.1 135 0062.ec29.70fe ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.220.88.30 220 5254.ab71.e119 ARPA FastEthernet4\n', 'Internet 10.220.88.32 191 5254.abc7.26aa ARPA FastEthernet4\n', 'Internet 10.220.88.33 190 5254.ab3a.8d26 ARPA FastEthernet4\n', 'Internet 10.220.88.35 175 5254.abfb.af12 ARPA FastEthernet4\n', 'Internet 10.220.88.38 159 0002.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.39 0 6464.9be8.08c8 ARPA FastEthernet4\n', 'Internet 10.220.88.40 144 001c.c4bf.826a ARPA FastEthernet4\n', 'Internet 10.220.88.41 123 001b.7873.5634 ARPA FastEthernet4\n', 'Internet 10.220.88.20 - c89c.1dea.0eb6 ARPA FastEthernet4\n', 'Internet 10.220.88.21 213 1c6a.7aaf.576c ARPA FastEthernet4\n', 'Internet 10.220.88.28 222 5254.aba8.9aea ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.111.11.11 69 abcd.efef.aaaa ARPA FastEthernet1\n'] >>> >>> arp_entries.sort() >>> pprint(arp_entries) ['\n', 'Internet 10.111.11.11 69 abcd.efef.aaaa ARPA FastEthernet1\n', 'Internet 10.220.88.1 135 0062.ec29.70fe ARPA FastEthernet4\n', 'Internet 10.220.88.20 - c89c.1dea.0eb6 ARPA FastEthernet4\n', 'Internet 10.220.88.21 213 1c6a.7aaf.576c ARPA FastEthernet4\n', 'Internet 10.220.88.28 222 5254.aba8.9aea ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.220.88.29 234 5254.abbe.5b7b ARPA FastEthernet4\n', 'Internet 10.220.88.30 220 5254.ab71.e119 ARPA FastEthernet4\n', 'Internet 10.220.88.32 191 5254.abc7.26aa ARPA FastEthernet4\n', 'Internet 10.220.88.33 190 5254.ab3a.8d26 ARPA FastEthernet4\n', 'Internet 10.220.88.35 175 5254.abfb.af12 ARPA FastEthernet4\n', 'Internet 10.220.88.37 69 0001.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.38 159 0002.00ff.0001 ARPA FastEthernet4\n', 'Internet 10.220.88.39 0 6464.9be8.08c8 ARPA FastEthernet4\n', 'Internet 10.220.88.40 144 001c.c4bf.826a ARPA FastEthernet4\n', 'Internet 10.220.88.41 123 001b.7873.5634 ARPA FastEthernet4\n', 'Protocol Address Age (min) Hardware Addr Type Interface\n'] |
There is also a sorted list function that can do the job if you want to keep the original list intact:
1 2 3 4 5 6 7 8 9 10 |
nic@vPackets.local:/Users/nic/github/Python3-Tests git:(master) $ python3 Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> original_list = ['3.3.3.3', '2.2.2.2', '1.1.1.1'] >>> new_list = sorted(original_list) >>> print(original_list) ['3.3.3.3', '2.2.2.2', '1.1.1.1'] >>> print(new_list) ['1.1.1.1', '2.2.2.2', '3.3.3.3'] |
I was testing this because I am currently working on the free python class that is run by Kirk Byers at https://pynet.twb-tech.com/ . To make the most of this course, I strongly recommend that course if you have a very small experience of programming. I will talk about that in a next blog post but in the meantime, have a look at kirk’s website. It’s awesome!
Thanks to Kirk, Nicholas Russo and Greg Mueller for the hints and help provided on slack ( Network to Code ran by Jason Edelman )
Nic