List contains tuple Python

DZone > Open Source Zone > Python Lists of Tuples

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.

by
Bill Ward
·
Aug. 05, 18 · Open Source Zone · Presentation
Like (3)
Comment
Save
Tweet
117.77K Views

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 withappend():

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 usingremove():

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.

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

Published at DZone with permission of Bill Ward, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

  • Service Discovery From Within the Kubernetes Cluster
  • PWA Push Notifications in JavaScript? Yes, You Can in 12 Steps!
  • Virtual Reality and Augmented Reality in the Browser
  • Load Balancing MQTT Broker Clustering Part 1

Comments

Open Source Partner Resources