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

preview_player
Показать описание
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:

#Python
Рекомендации по теме
Комментарии
Автор

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.

fupopanda
Автор

As precise, helpful and enjoyable as always - even after 6.5 years. My favorite Python YT resource. Happy to be a member. Thanks Corey!

bartuslongus
Автор

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.

xcxfgpn
Автор

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

vikramsarabhai
Автор

I am liking all your Python videos. Well done and I am not exaggerating. (takes his hat off and applause)

jokerMCify
Автор

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)

shaunsawyer
Автор

this is a very small tip but sometimes it is very important to know these small tips. thanks for being with us dear @Cory Schafer.

alixaprodev
Автор

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

onuberonly
Автор

Thank you Corey. Great video, very useful.

MehmetMustafaICER
Автор

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

mbasith
Автор

Love your channel!!! Please keep it up :)

vaibhavkrkm
Автор

Very good explanation and easy to follow. Thank you.

julia_orchid_cali
Автор

it seems its what I looking for :) Thank you!

slonbeskonechen
Автор

you can also add a built-in function in to namedtuple filed. For example
# define a namedtuple with func
Colorf = namedtuple('Colorf', 'r g b fun')
clrf_ntpl = Colorf(1, 2, 3, sum)
print("namedtuple clrf_ntpl.fun([1, 10]): ", clrf_ntpl.fun([1, 10]))

wangleimail
Автор

never even heard of these, but they look pretty cool. thanks!

snpsforyomom
Автор

Color = namedtuple("Color", ["x", "y", "z"])
What's the difference between variable Color and argument "Color"?
I found "Color" argument can be anything and namedtuple still works fine. So what's the purpose of this argument?

RanjeetkumarYadav
Автор

Maaan... you are more easy than the documentation.

fahadmdkamal
Автор

for python 3, you have to do:
from collections import namedtuple

waseksamin
Автор

what is the significance of the text 'Color' within namedtuple('Color', [...]), should this be same as the variable Color. If its not the same, what are the consequences.
Thanks

DilipKumar-iccg
Автор

If I want to add another attribute to the tuple, such as 'purple' do I need to create a new tuple/

arcsaber
visit shbcf.ru