简体   繁体   中英

Print list item with in a list in python

if i have a list example

x = [1,2,3,["a","b","c"]]

How can I print from stack list within the list

If you want to only print the elements within the nested list you can do:

x = [1,2,3,["a","b","c"]]
for elem in x:
    if isinstance(elem, list):
        print(*elem)

Edit If you have a desired element from within the nested list:

desired_elem = "c"
for elem in x:
    if isinstance(elem, list):
        print(elem[elem.index(desired_elem)])

And of course that you can just reach that element using indexing.

print(x[2][1])

You can access individual elements of a list by using [i] if the element received is also a list you can just access again:

x = [1,2,3,["a","b","c"]]
print(x[3][2])

outputs c

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM