Python Forum
Print Nested List Line by Line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Nested List Line by Line
#1
Hello,

I have the following nested list:

>>> print(list_mounts)
[['/var', 71], ['/u01', 61], ['/backups', 69]]
I'd like to print the list with the following output:

Drive: /var Used Space: 60
Drive: /u01 Used Space: 60
Drive: /backups Used Space: 60

I tried the following code which gives me the first two columns above:

>>> for i in list_mounts:
...     print("Drive: ", i[0])
...
Drive:  /var
Drive:  /u01
Drive:  /backups
...but when I try the following code, I receive errors:

>>> for x,y in list_mounts:
...     print("Drive: ", x[0], "Used Space: ", y[0])
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: 'int' object is not subscriptable
>>> for x,y in list_mounts(x,y):
...     print("Drive: ", x[0], "Used Space: ", y[0])
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> for x,y in list_mounts[x,y]:
...     print("Drive: ", x[0], "Used Space: ", y[0])
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not tuple
I'm at a standstill trying to figure this out. Been trying different loops and searching Google to no avail.

Any help would be greatly appreaciated.

Thanks!
Reply


Messages In This Thread
Print Nested List Line by Line - by anelliaf - May-23-2018, 06:19 PM
RE: Print Nested List Line by Line - by ThiefOfTime - May-23-2018, 06:37 PM
RE: Print Nested List Line by Line - by anelliaf - May-23-2018, 06:38 PM
RE: Print Nested List Line by Line - by ThiefOfTime - May-23-2018, 06:41 PM
RE: Print Nested List Line by Line - by anelliaf - May-23-2018, 06:44 PM
RE: Print Nested List Line by Line - by buran - May-23-2018, 06:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 6 423 Yesterday, 08:01 AM
Last Post: arbiel
  How to add multi-line comment section? Winfried 3 507 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 530 May-23-2024, 03:02 PM
Last Post: deanhystad
  Line graph with two superimposed lines sawtooth500 4 537 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  break print_format lengthy line akbarza 4 550 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,561 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 673 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 555 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 461 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 1,088 Jan-16-2024, 03:45 AM
Last Post: johnywhy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020