Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Python Lists of Tuples

DZone's Guide to

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.

· Open Source Zone ·
Free Resource

New Report Reveals Open Source Risk Is Still a Mystery to Many. Read more.

In this post we will talk about creating Python Lists of Tuples and how they can be used.

Python Lists

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 [6]: awesomeList.append("warlock")

In [7]: awesomeList
Out[7]: ['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 [7]: awesomeList
Out[7]: ['paladin', 'rogue', 'priest', 'warrior', 'druid', 'warlock']

In [8]: awesomeList[0]
Out[8]: 'paladin'

In [9]: awesomeList[3]
Out[9]: 'warrior'


Change items by using the offset as well:

In [10]: awesomeList[3] = "monk"

In [11]: awesomeList
Out[11]: ['paladin', 'rogue', 'priest', 'monk', 'druid', 'warlock']


Lastly, you can delete items from the list by using remove() : 

In [12]: awesomeList.remove('monk')

In [13]: awesomeList
Out[13]: ['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:

Python 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 [14]: awesomeTuple = ('paladin', 'rogue', 'priest', 'warrior', 'druid')

In [15]: awesomeTuple
Out[15]: ('paladin', 'rogue', 'priest', 'warrior', 'druid')


With tuples we can "unpack" the values like this:

In [16]: belkas, gorkin, landril, maxilum, ferral = awesomeTuple

In [17]: belkas
Out[17]: 'paladin'

In [18]: maxilum
Out[18]: 'warrior'


You can also create a tuple from a list.

In [20]: tuple(awesomeList)
Out[20]: ('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 [21]: 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 [29]: errorCount = 0
    ...: for log in logs:
    ...:     status, message = log
    ...:     if status is not 'HTTP_OK':
    ...:         errorCount += 1
    ...:         

In [30]: errorCount
Out[30]: 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.

Software composition Analysis for DevSecOps. Start finding vulnerabilities in your open source components today.

Topics:
python ,lists ,tuples ,code ,data structures ,open source

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}