Python Lists of Tuples
In Python, both lists and tuples are handy data structures. Let's see how we can combine them into another useful construct.
Join the DZone community and get the full member experience.Join For Free
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
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 offset. 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
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')
Check out 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.
Let's say we parsed in a log file and we have the status code and message from an Apache2 weblog.
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.
I hope you have enjoyed this article.
Published at DZone with permission of Bill Ward, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.