0

A function is returning me the below list of named tuples:

[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

I want to print the value of 'path' in the tuple. How to do this? Please help dear community.

I tried to follow online articles but no solve.

7
  • 1
    Does this answer your question? What are "named tuples" in Python? Commented May 28, 2023 at 16:19
  • These examples show how to add values in tuple and then access that. But, I want to access & print values of named tuples that I found from another function.
    – Saga Harby
    Commented May 28, 2023 at 16:23
  • I don't understand what you're asking, these examples have already shown you how to access values. Commented May 28, 2023 at 16:29
  • I can't understand how to access each popenfile tuple and its value using for loop.
    – Saga Harby
    Commented May 28, 2023 at 16:38
  • So you should ask or search how to use the for loop, a simple example: for tpl in your_list: print(tpl.path) Commented May 28, 2023 at 16:45

1 Answer 1

1

To make it clear see below code:

from collections import namedtuple

popenfile = namedtuple('popenfile', 'path fd position mode flags')

tuple_list = [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

for ntup in tuple_list:
    print(ntup.path)

which prints

/home/giampaolo/monit.py
/var/log/monit.log

Not the answer you're looking for? Browse other questions tagged or ask your own question.