In this post we will talk about creating Python Lists of Tuples and how they can be used.
Lists in Python are simply an array. Here is a basic list of my favorite WoW Classes:
awesomeList = ['paladin', 'rogue', 'priest', 'warrior', 'druid']
Lists are created using brackets
We can add stuff to the end of our list with append()
In : awesomeList.append("warlock") In : awesomeList Out: ['paladin', 'rogue', 'priest', 'warrior', 'druid', 'warlock']
Items in a list have an index starting at 0 called an offest. So we can reference specific items in our list like this:
In : awesomeList Out: ['paladin', 'rogue', 'priest', 'warrior', 'druid', 'warlock'] In : awesomeList Out: 'paladin' In : awesomeList Out: 'warrior'
Change items by using the offset as well:
In : awesomeList = "monk" In : awesomeList Out: ['paladin', 'rogue', 'priest', 'monk', 'druid', 'warlock']
Lastly, you can delete items from the list by using remove()
In : awesomeList.remove('monk') In : awesomeList Out: ['paladin', 'rogue', 'priest', 'druid', 'warlock']
There is more to lists but that should be enough for the purposes of this post. You can learn more from the Python reference documentation if you wish. Onward to tuples:
Tuples are very similar to lists, but tuples are immutable. This means after they are created you can’t change them.
Let’s create a tuple from the same list of WoW classes above.
In : awesomeTuple = ('paladin', 'rogue', 'priest', 'warrior', 'druid') In : awesomeTuple Out: ('paladin', 'rogue', 'priest', 'warrior', 'druid')
With tuples we can “unpack” the values like this:
In : belkas, gorkin, landril, maxilum, ferral = awesomeTuple In : belkas Out: 'paladin' In : maxilum Out: 'warrior'
You can also create a tuple from a list.
In : tuple(awesomeList) Out: ('paladin', 'rogue', 'priest', 'druid', 'warlock')
Checkout the Python reference documentation for more info on tuples.
Now that we have a good intro to Python Lists and Tuples we can get to the meat of this tutorial.
Python Lists of Tuples
We can create lists of tuples. This is great for working with stuff like log files.
Lets say we parsed in a log file and we have the status code and message from an Apache2 web log.
We could then represent this data using python lists of tuples. Here is an overly-simplified example:
In : logs = [ ...: ('HTTP_OK', 'GET /index.html'), ...: ('HTTP_NOT_FOUND', 'GET /index.htmll') ...: ]
This lets us do some pretty cool operations like count the number of errors.
In : errorCount = 0 ...: for log in logs: ...: status, message = log ...: if status is not 'HTTP_OK': ...: errorCount += 1 ...: In : errorCount Out: 1
Why use tuples over lists? Tuples use less space for one. Using the example above for parsing a log file, if the log file is big then using tuples reduces the amount of memory used.
Need more Python training?
Checkout these awesome Python courses on Pluralsight. Pluralsight has great video training courses at a great price. I use Pluralsight every time I need to learn something new.
I hope you have enjoyed this article. If you did then please share it using the buttons on the left.