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
#2
Your idea is good. The thing you have to consider is that when you say
for i in list_mounts
you are picking each line at once out of the list. If you would say:
for x, y in list_mounts
you are picking each line and save the elements of the gotten line to x and y
so simply change your code to:
for x,y in list_mounts:
     print("Drive: ", x, "Used Space: ", y)
x and y are your values contained in the nested lists so that y becomes an integer so that you can not iterate over this (that was the first error.
The second error was due to you trying to call the list like a function list_mounts(x, y), but it is an Object of List and not a function, therefore you get the "object not callable" error.
The third error was due to the fact that you called a list with [x, y] x and y are interpreted as a tuple and that cannot be applied to lists, but it can to arrays ;) since it needs an integer it throws the "indices must be integers or slices, not tuple" error.
Reply
#3
Update:

I was able to print the values, line by line:

>>> for i in range(len(list_mounts)):
...     for j in range(len(list_mounts[i])):
...             print(list_mounts[i][j], end = ' ')
...     print()
...
/var 71
/u01 61
/backups 69
Now I need to add formatting to get the end result. Still looking for input.

Thanks!
Reply
#4
just use one loop, like in my post before. since you are iterating once over the array you can get both values at once :)
Another way to do it is to
for i in list_mounts:
     print("Drive: ", i[0], "Used Space: ", i[1])
Reply
#5
Yes, thanks! I seen your other post after I posted my update.

Your advice/code gave me an understanding of what I was doing wrong. Much appreciated!

>>> for x,y in list_mounts:
...     print("Drive: ", x, "Used Space: ", y)
...
Drive:  /var Used Space:  71
Drive:  /u01 Used Space:  61
Drive:  /backups Used Space:  69
>>> for i in list_mounts:
...     print("Drive: ", i[0], "Used Space: ", i[1])
...
Drive:  /var Used Space:  71
Drive:  /u01 Used Space:  61
Drive:  /backups Used Space:  69
>>>
Reply
#6
I see you already corrected it but anyways - read https://python-forum.io/Thread-Basic-Nev...n-sequence
by the way, avoid using cryptic one-letter variable names. There are limited number of cases when it's fine to use such variable names,
but in this case something like:
for drive, space in list_mounts:
    print("Drive: {}, Used Space: {}".format(drive, space))
makes your code much readable
in python 3.6+ you can even take advantage of new f-strings
for drive, space in list_mounts:
    print(f"Drive: {drive}, Used Space: {space}")
finally, you can also use
for drive in list_mounts:
    print("Drive: {}, Used Space: {}".format(*drive))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 139 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 275 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,148 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 419 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 336 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 273 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 609 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Reading in of line not working? garynewport 2 786 Sep-19-2023, 02:22 PM
Last Post: snippsat
  'answers 2' is not defined on line 27 0814uu 4 671 Sep-02-2023, 11:02 PM
Last Post: 0814uu
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 555 Aug-22-2023, 10:14 AM
Last Post: GYKR

Forum Jump:

User Panel Messages

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