เมื่อต้องการหาตำแหน่งที่ใกล้ที่สุดระหว่างสององค์ประกอบในลิสต์ ใช้เมธอดที่รับสามพารามิเตอร์ ใช้ not in และ list comprehension เพื่อหาผลลัพธ์ สาธิตด้านล่าง: ตัวอย่าง def nearest_occurence_list(my_list, x, y): if x not in my_list or y not in my_list: return -1 x_index = [index for i