Amit Pal
Amit Pal

Reputation: 11052

How to execute two loop simultaneously in python?

I have two lists:

 host_list = ["10.3.11.250", "10.3.24.45", "10.5.3.5","10.3.4.5"]
 ip_value = ["34.45.34.5", "10.3.11.250","10.3.4.5"]

I want to check whether the data of host_list is present in ip_value or not if it is then append the ip_value to another list. I am doing in this way check the following code:

for host,ip in zip(host_list ,ip_value):
    if host_list == ip_value
        list_ip = list_ip.append(ip)

But it does nothing.Why? and what should list_ip returns it will returns: {"10.3.11.250", "10.3.4.5"}

Upvotes: 0

Views: 201

Answers (3)

Pierce
Pierce

Reputation: 564

To answer the question, why does the code you give do nothing:

for host,ip in zip(host_list ,ip_value): 
    if host_list == ip_value 
        list_ip = list_ip.append(ip)

You are comparing host_list to ip_value, and not comparing host to ip. host_list != ip_value, thus the next statement is never executed.

Upvotes: 1

eumiro
eumiro

Reputation: 212885

These are sets, not lists. You can calculate a difference of them:

list_ip = host_list - ip_value

returns

{'10.5.3.5', '10.3.24.45'}

Edited: ok, now they are two lists. Change the code to:

list_ip = list(set(host_list) - set(ip_value))

returns

['10.5.3.5', '10.3.24.45']

Upvotes: 6

0xc0de
0xc0de

Reputation: 8287

Use sets

another_list = list(set(host_list) - set(ip_value))

Upvotes: 2

Related Questions