Click here to Skip to main content
15,885,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ls=[1,2,3,5,6,['1stlayer',1,2,3,4,5,['2ndlayer',1,2,3,4,5,['3rdlayer',1,2,3,4,5,['4thlayer',1,2,3,4,5,['5thlayer',1,2,3,4,5,['6thlayer',1,2,3,4,5,['7thlayer',1,2,3,4,5]]]]]]]]


print second layer


What I have tried:

i've tried doing the indexing like
print(ls[2][0])
but it gives me an error stating
'int' object is not subscriptable
Posted
Updated 23-Feb-22 2:48am

Look at your data (and it's kinda odd).
What is element 2 of ls?
Answer:
Python
ls[0] is 1,
ls[1] is 2,
ls[2] is 3,
...
And ls[5] is:
['1stlayer',1,2,3,4,5,['2ndlayer',1,2,3,4,5,['3rdlayer',1,2,3,4,5,['4thlayer',1,2,3,4,5,['5thlayer',1,2,3,4,5,['6thlayer',1,2,3,4,5,['7thlayer',1,2,3,4,5]]]]]]]

I suspect that you need to rethink exactly what your data should look like; I have no idea what you are trying to do, but that all looks very, very wrong!
 
Share this answer
 
v2
Comments
Member 15356357 23-Feb-22 8:32am    
actually my teacher gives us in that way and we cant change the format or anything but we are only allowed to print the specific block from that list.. now idk how to print the specific block without printing the rest of the list. as ive said there i tried using indexing and it only gives me an error. i would really appreciate it if you could teach me.
Member 15356357 23-Feb-22 8:33am    
the specific block i mean is like this ['1stlayer',1,2,3,4,5,
OriginalGriff 23-Feb-22 9:07am    
You have to range it:
print(ls[5][0:6])
Richard MacCutchan 23-Feb-22 8:49am    
It's valid Python, even if it does make your eyes go weird.
OriginalGriff 23-Feb-22 9:04am    
Oh, I know - I ran it through a online Python interpreter to check what I was saying.

But it's very poor data design! :laugh:
You need to get used to understanding the relative positions of each item in the list(s). The item you are interested in starts at position 5 of ls. And that item contains 6 elements. So the print statement you need is:
Python
print(ls[5][:6])

That is to say, print the first 6 elements of the element at offset 5. Try a few other values for yourself.

[edit]
It occurred to me on re-reading this that the above statement might make more sense if re-written as:
Select the item at offset 5 of ls, and print its first 6 items.
[/edit]
 
Share this answer
 
v2
Comments
Member 15356357 23-Feb-22 8:51am    
thank you so much for the help..i understand now

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900