💡
#PythonTips #DataStructures #collections #namedtuple #Python
---
By: @DataScienceQ ✨
collections.namedtuple for structured data: Create simple, immutable data structures without boilerplate.from collections import namedtuple
Define a simple Point structure
Point = namedtuple('Point', ['x', 'y'])
Create instances
p1 = Point(10, 20)
p2 = Point(x=30, y=40)
print(f"Point 1: x={p1.x}, y={p1.y}")
print(f"Point 2: {p2[0]}, {p2[1]}") # Access by index
It's still a tuple!
print(f"Is p1 a tuple? {isinstance(p1, tuple)}")
Example with a Person
Person = namedtuple('Person', 'name age city')
person = Person('Alice', 30, 'New York')
print(f"Person: {person.name} is {person.age} from {person.city}")
#PythonTips #DataStructures #collections #namedtuple #Python
---
By: @DataScienceQ ✨
❤1