Records with namedtuple

EasyCollections Programs · 13 of 20

Write a Python program to store employee records as named tuples.

Advertisement
Output
Asha QA 52000
Ravi Dev 61000
Ravi

Approach

  1. namedtuple("Employee", [...]).

Solution

Python
from collections import namedtuple Employee = namedtuple("Employee", ["name", "dept", "salary"])staff = [Employee("Asha", "QA", 52000), Employee("Ravi", "Dev", 61000)]for e in staff:    print(e.name, e.dept, e.salary)print(max(staff, key=lambda e: e.salary).name)
Output
Asha QA 52000
Ravi Dev 61000
Ravi
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More collections programs

← All 20 Collections Programs