I am using Python. Given a list, I want to print the contents of all the sublists in the original list. So, for instance, if I have a list:
L = ["0", ["(1,0)"], ["(2,0)", ["(2,1,0)", ["(2,1,1,0)"], ["(2,1,1,1)"]]], ["(3,0)"]]
Then I want my output to be:
0
(1,0)
(2,0)
(2,1,0)
(2,2,2,0)
(2,1,1,1)
(3,0)
Now, I have used the following code:
def Print_Nested_List(s):
a = 0
for a in range(len(s)):
if type(s[a]) != type([]):
print(s[a])
else:
b = 0
for b in range(len(s[a])):
Print_Nested_List(s[a])
L = ["0", ["(1,0)"], ["(2,0)", ["(2,1,0)", ["(2,1,1,0)"], ["(2,1,1,1)"]]], ["(3,0)"]]
Print_Nested_List(L)
I get the following output:
0
(1,0)
(2,0)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(2,0)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(2,1,0)
(2,1,1,0)
(2,1,1,1)
(3,0)
Clearly, you can see that although all the contents are printed, some stuff is printed multiple times.
Now, why is this happening and how can I fix my code?
Note: I am just a noob at coding and any help would be great. Is there a better way of doing this? Thanks a lot in advance:)