Python Data Science Jobs & Interviews
20.4K subscribers
188 photos
4 videos
25 files
328 links
Your go-to hub for Python and Data Science—featuring questions, answers, quizzes, and interview tips to sharpen your skills and boost your career in the data-driven world.

Admin: @Hussein_Sheikho
Download Telegram
💡 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