import socket ips = [] with open('ips.txt', 'w') as out: for line in open('domains.txt'): l = line.strip() if l.startswith('#') or l == '': continue ips.append(socket.gethostbyname(l)) ips = list(dict.fromkeys(ips)) for ip in ips: out.write(ip + '\n')