Print Nested List Line by Line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Print Nested List Line by Line (/thread-10515.html) |
Print Nested List Line by Line - anelliaf - May-23-2018 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 tupleI'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! RE: Print Nested List Line by Line - ThiefOfTime - May-23-2018 Your idea is good. The thing you have to consider is that when you say for i in list_mountsyou are picking each line at once out of the list. If you would say: for x, y in list_mountsyou 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. RE: Print Nested List Line by Line - anelliaf - May-23-2018 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 69Now I need to add formatting to get the end result. Still looking for input. Thanks! RE: Print Nested List Line by Line - ThiefOfTime - May-23-2018 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]) RE: Print Nested List Line by Line - anelliaf - May-23-2018 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 >>> RE: Print Nested List Line by Line - buran - May-23-2018 I see you already corrected it but anyways - read https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-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)) |