Is there a way to be able to access specific sublist element?
For example:
list = [["a","b"],["c","d"]]
how to print out only b
??
Thanks!
Firstly, it is a bad practice to name a variable as list
as it is a keyword in Python. Say, it is changed to lis
lis = [["a","b"],["c","d"]]
To access b, you need to first get to the first element of lis
, by lis[0]
. This gives ["a", "b"]
. Now you need to further go into it, so you do lis[0][1]
, This gives the 1-indexed element of lis[0]
, ie, 'b'
You need to select the first sublist in your list
(index 0), and then the second element of your sublist (index 1).
Result is list[0][1]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.