What is a tuple?
Tuples are very similar to lists, but it is unable to be changed (immutable). This provides a tiny performance and memory savings. More importantly provides a hint to the interpreter and some code protection when you know a data structure shouldn't be changing.
The difference in performance is not something you should code for unless you are working with extremely large datasets. The fact they are immutable is the main reason to use tuples over a list.
The syntax of a tuple is almost exactly like a list, except you use (
)
instead of [
]
.
List
['Bob Jones', 'Manager', 'Denver']
Tuple
('Bob Jones', 'Manager', 'Denver')
What is a named tuple
A named tuple is a module from the collection class that allows you to name the elements in a tuple so they can act as a lightweight class or enum.
Example Usage
from collections import namedtuple
Employee = namedtuple('Employee', ['name', 'role', 'location'])
bob = Employee('Bob Jones', 'Manager', 'Denver')
print(bob.name)
collections is a default package and you do not need to install it, but you will need to import it. Accessing the name of an Employee data set is really easy and readable. It makes for easier to read code and fewer bugs and is very lightweight. In some text editors/IDEs it can also provide code insight and auto-completion.
If you have objects that should not be changing and don't require methods (class functions) then tuples are likely are a better choice than a list.
My Python Tips Series
- f-strings in Python 3.6
- Underscores in numeric literals
- A better interactive shell
- Secrets Module - New in 3.6
- PEP 8
- Slices
Why you should vote me as witness
Witness & Administrator of four full nodes
My recent popular posts
STEEM, STEEM Power, Vests, and Steem Dollars. wtf is this shit?
The truth and lies about 25% curation, why what you know is FAKE NEWS
WTF is a hardware wallet, and why should you have one?
GINABOT - The Secret to your Sanity on Steemit
How to calculate post rewards
Use SSH all the time? Time for a big boy SSH Client
How to change your recovery account
How curation rewards work and how to be a kick ass curator
Markdown 101 - How to make kick ass posts on Steemit
Work ON your business, not in your business! - How to succeed as a small business
You are not entitled to an audience, you need to earn it!
How to properly setup SSH Key Authentication - If you are logging into your server with root, you are doing it wrong!
Building a Portable Game Console
https://steemit.com/steemit/@michaellamden68/themarkymark
Thank you @themarkymark. Your tips are so great to the programming community of Steemit.
Sir I am very interested in learning programming. i know html. now I want to know python.
your post inspire me to learn it.
thank you very much sir for your valuable post.
Hi nice article.....Keep writing that's like article and let's help each other also checkout my post click here
Can you comment on when is it better to use a namedtuple vs a dictionary and vice versa?
A named tuple does not require a key and is not required to be hashable. Named tuples are ordered, unlike a dict. You can also use dot notation instead of [' '] which is a bit easier to type and has some readability improvements.
Thanks! I use python for data analysis but rarely use things like namedtuple and dictionary. By ordered I suppose you mean that if I want to, I can iterate through a namedtuple but not a dictionary?
When you use a dict, there is no guarantee you will get the keys in the order you created them. Access is typically by key so that's not important.
WIth named tuple, you know the first element, will always be returned first.
Cool! Thanks for the tips!
Thanks for this lecture on tuple. Programming is life
Congratulations @themarkymark! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of comments
Click on any badge to view your own Board of Honor on SteemitBoard.
To support your work, I also upvoted your post!
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Is @upmyvote your bot ? I transfered $3 SBD before the last round ended but never got my vote. I skimmed it's recent article ( 3 months old ) and seen you replying. Sorry to hijack your topic.
Sent a refund, please use our #support on Discord in the future.
Thanks !