Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python namedtuple

import collections

# create a named tuple called Person with fields of first_name, last_name, and age
Person = collections.namedtuple('Person', ('first_name', 'last_name', 'age'))

# Note a named tuple can have fields names as string using a space as a delimiter also see example below
Person = collections.namedtuple('Person', 'first_name last_name age')

# initialize a user as a Person Tuple
user = Person(first_name="John", last_name="Doe", age=21)

# print user named tuple
print(user)

# print user by field names
print(user.first_name, user.last_name, user.age)

# print length of user
print(len(user))

# print first name by index
print(user[0])

# loop through user
for item in user:
   print(item)

Comment

namedtuple python

# Python code to demonstrate namedtuple() and
# _make(), _asdict() and "**" operator
 
# importing "collections" for namedtuple()
import collections
 
# Declaring namedtuple()
Student = collections.namedtuple('Student',
                                 ['name', 'age', 'DOB'])
 
# Adding values
S = Student('Nandini', '19', '2541997')
 
# initializing iterable
li = ['Manjeet', '19', '411997']
 
# initializing dict
di = {'name': "Nikhil", 'age': 19, 'DOB': '1391997'}
 
# using _make() to return namedtuple()
print("The namedtuple instance using iterable is  : ")
print(Student._make(li))
 
# using _asdict() to return an OrderedDict()
print("The OrderedDict instance using namedtuple is  : ")
print(S._asdict())
 
# using ** operator to return namedtuple from dictionary
print("The namedtuple instance from dict is  : ")
print(Student(**di))
Comment

Python Namedtuples

# Using namedtuple is way shorter than
# defining a class manually:
>>> from collections import namedtuple
>>> Car = namedtuple('Car', 'color mileage')

# Our new "Car" class works as expected:
>>> my_car = Car('red', 3812.4)
>>> my_car.color
'red'
>>> my_car.mileage
3812.4

# We get a nice string repr for free:
>>> my_car
Car(color='red' , mileage=3812.4)

# Like tuples, namedtuples are immutable:
>>> my_car.color = 'blue'
AttributeError: "can't set attribute"
Comment

PREVIOUS NEXT
Code Example
Python :: django model query join 
Python :: if key in dictionary python 
Python :: quantile calcultion using pandas 
Python :: get first letter of each word in string python 
Python :: find & replace in csv file 
Python :: kdeplot python 
Python :: Appending rows to a DataFrame 
Python :: first and last name generator python 
Python :: sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 7 supplied. 
Python :: dockerfile to run python script 
Python :: python Sort the dictionary based on values 
Python :: python filter list with lambda 
Python :: python program to check whether a number is even or odd 
Python :: print data type array 
Python :: how to add value in array django 
Python :: python tree 
Python :: Python how to use __mul__ 
Python :: web socket in python 
Python :: python glob.glob recursive 
Python :: python find if string contains space 
Python :: multiple inputs in one line- python 
Python :: radix sort strings python 
Python :: python machine learning 
Python :: boolean in python 
Python :: Python Create a nonlocal variable 
Python :: python regions 
Python :: python print every character in list as string 
Python :: function in python 
Python :: current page django 
Python :: import permutations 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =