Python Tutorial: Namedtuple – When and why should you use namedtuples?

///Python Tutorial: Namedtuple – When and why should you use namedtuples?

Python Tutorial: Namedtuple – When and why should you use namedtuples?

FavoriteLoadingAdd to favorites

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

By |2020-03-22T02:30:08+00:00March 22nd, 2020|Python Video Tutorials|25 Comments

25 Comments

  1. 팍준하 March 22, 2020 at 2:30 am - Reply

    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!

  2. 팍준하 March 22, 2020 at 2:30 am - Reply

    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.

  3. dastaan Jamshedpurian March 22, 2020 at 2:30 am - Reply

    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

  4. Kiran March 22, 2020 at 2:30 am - Reply

    Thanks Corey, this helped a lot

  5. sujith kumar March 22, 2020 at 2:30 am - Reply

    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

  6. Ándý March 22, 2020 at 2:30 am - Reply

    Still a great video, tank you!

  7. Mehmet Mustafa ICER March 22, 2020 at 2:30 am - Reply

    Thank you Corey. Great video, very useful.

  8. Selvi Perumal March 22, 2020 at 2:30 am - Reply

    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.

  9. rowida nagah March 22, 2020 at 2:30 am - Reply

    wondering how to print any of this color as its value beside

  10. Tren Black March 22, 2020 at 2:30 am - Reply

    You have an aptitude for teaching

  11. Ben Nguyen March 22, 2020 at 2:30 am - Reply

    Is there a way to get the functionality of the namedtuple, but that is changeable (i.e. not immutable)?

  12. Fabio Classo March 22, 2020 at 2:30 am - Reply

    Nice Tip!

  13. Shanmuga Raj March 22, 2020 at 2:30 am - Reply

    Thanks Corey. So basically this is creating a new sub class called Color and we are instantiating an instance named color. Right?

  14. Austin Lam March 22, 2020 at 2:30 am - Reply

    color = Color(55, 155, 255)

    How did Color become a function?

  15. Joe Tang March 22, 2020 at 2:30 am - Reply

    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.

  16. d p March 22, 2020 at 2:30 am - Reply

    Awesome as always. Btw, has anyone ever said you sound like Jeremy from Geeks&Gamers YouTube channel? 🤔🤔🤔

  17. fupopanda March 22, 2020 at 2:30 am - Reply

    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.

  18. Mehmet Ali Karabulut March 22, 2020 at 2:30 am - Reply

    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?)…

  19. meir fishman March 22, 2020 at 2:30 am - Reply

    This was worth every second of my time.
    Thank You So Much.

  20. So Amazing! March 22, 2020 at 2:30 am - Reply

    its like a c/c++ struct I love it now I have a way to use a struct like object in python…Powerful!

  21. Shaun Sawyer March 22, 2020 at 2:30 am - Reply

    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)

  22. sudipta samanta March 22, 2020 at 2:30 am - Reply

    Can you do a video on collections module ?

  23. steve frt March 22, 2020 at 2:30 am - Reply

    Thanks you so much

  24. M Yousuffi March 22, 2020 at 2:30 am - Reply

    This was very helpful for me. Thanks for posting!!!

  25. imxron March 22, 2020 at 2:30 am - Reply

    Why would we want to use a NamedTuple over just a color class? Because it's immutable? Thanks for the great video!

Leave A Comment

*