Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,868 questions

51,791 answers

573 users

How to find minimum distance between two numbers in a list with Python

1 Answer

0 votes
import sys

def MinDistance(lst, number1, number2):
    min_distance = sys.maxsize
    size = len(lst)
    
    for i in range(size):
        for j in range(i + 1, size):
            if (lst[i] == number1 and lst[j] == number2 or 
                lst[i] == number2 and lst[j] == number1):
                min_distance = min(min_distance, abs(i - j));        
    
    return min_distance
 
 
lst = [2, 3, 7, 3, 3, 4, 8, 8, 8, 11, 9, 1, 3]

print(MinDistance(lst, 3, 8))



'''
run:

2

'''

 



answered Aug 24, 2022 by avibootz
...