DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. 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.

Bill Ward user avatar by
Bill Ward
·
Aug. 05, 18 · Presentation
Like (3)
Save
Tweet
Share
119.08K Views

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.

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.

Tuple Python (language)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • An Introduction to Data Mesh
  • How To Create and Edit Excel XLSX Documents in Java
  • Why It Is Important To Have an Ownership as a DevOps Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: