

Named Tuples in Python are High-performance container datatypes. What advantage do namedtuples have over regular tuples and when should you use them? In this video, we’ll take a look at namedtuples and why you should use them.
The code from this video can be found at:
✅ Support My Channel Through Patreon:
✅ Become a Channel Member:
✅ One-Time Contribution Through PayPal:
✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
✅ Corey’s Public Amazon Wishlist
✅ Equipment I Use and Books I Recommend:
▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –
#Python
source
I have a problem follwing the tutorial.
I use python 3.7, and whenever I do
print(color['red'])
like in the video, the console says I'm in an error where index can only be integers. However, when I do
print(color.red)
, 255 does get printed out on the console. I don't understad why Corey can do it but not me. Also, I do not understand how corey can use the print function without using the parenthesis. In my system, I have to do
print('hello world')
, and
print 'hello world'
does not work.:d
Can someone help me find whats' wrong with me?
(or what makes my python environment different from Corey's)
Thank you!
Summary:
Sometimes we need something between a tuple and a dictionary.
Tuples have less typing to do but it is easy to forget what each values stand for. In contrast, Dictionaries are very wordy, while we can assign the keys to what the value represents.
Using named tuple makes the meaning of each value explicit while not being as wordy as Dictionary.
How:
1. from collections import namedtuple
2. set a template of the namedtuple, as below.
Color = namedtuple('Color', ['red', 'blue', 'green'])
3. create a named tuple using that template, as below
red_color = Color(255, 0, 0)
4. now a namedtuple is created. We can use indexing to access each value, but can also add '.red', '.blue', or '.green' at the end of the name of tuple, to access each value, too.
print(red_color.red)
print(red_color[0])
both will print out 255 in the console.
Tuitorial is amazing , All of you guys practice above knowledge in android, Setup of python in android can learn from this video https://youtu.be/1mCKcQZkxh4
Thanks Corey, this helped a lot
0:05 bro how could you import namedtuple from collections i can't understand how to import is there any other vedio for how to import namedtuple from collections plss replyy plsss
Still a great video, tank you!
Thank you Corey. Great video, very useful.
hi
I would to create player name, scoring pad and dice they use in dice rolling game for multiplayer using named tuple. I am bit of confused how to create that one. can u guide me.
wondering how to print any of this color as its value beside
You have an aptitude for teaching
Is there a way to get the functionality of the namedtuple, but that is changeable (i.e. not immutable)?
Nice Tip!
Thanks Corey. So basically this is creating a new sub class called Color and we are instantiating an instance named color. Right?
color = Color(55, 155, 255)
How did Color become a function?
RGB example is very simple, especially I like the white color value assignment, lots of fun. I wish I watched this video and other Corey's videos since 2015, I would have been a Python expert now.
Awesome as always. Btw, has anyone ever said you sound like Jeremy from Geeks&Gamers YouTube channel? 🤔🤔🤔
For those experienced with using OOP, here is some helpful information not mentioned in this video:
namedtuple returns a class (that's a child of the built-in class tuple). The first argument you pass to namedtuple becomes the name of the class, while the list of strings becomes the attributes (data fields). You can then call the constructor (the line of code before the print() method in the video) to make objects.
Coming from a fully object oriented programming background (Java!), I wonder why do they invent something like namedtuple instead of just using classes and objects, just like below:
class Color:
def __init__(self,red,green,blue):
self.red=red
self.green=green
self.blue=blue
color = Color(55,155,255)
print(color.red)
# is this too long to type? I bet it is more readable and understandable in OOP world (python is OOP too, isnt it?)…
This was worth every second of my time.
Thank You So Much.
its like a c/c++ struct I love it now I have a way to use a struct like object in python…Powerful!
Should also note printing named tuples gives you a very clear representation of what they are and what they contain. For instance,
>> Position = namedtuple('Coordinate', ['x', 'y'])
>> p1 = Position(1, 3)
>> print p1
Coordinate(x=1, y=3)
Can you do a video on collections module ?
Thanks you so much
This was very helpful for me. Thanks for posting!!!
Why would we want to use a NamedTuple over just a color class? Because it's immutable? Thanks for the great video!