Python takes the upper and lower elements of an element in list

how do I get the upper and lower elements of an element in list? Thank you for your advice.

Mar.23,2021

if you are using the index to locate the element, such as l [I] , you should be able to get it via l [item1] , l [I-1] . If not, you can get the index first and then through the index.


if positioned by this element, pre, last = l [l.index (a)-1], l [l.index (a) + 1]

Menu