0

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"}

4
  • But more importantly, are the items in host_list some kind of IP objects? Normal strings do not have a .ipv4 or .ipv6 attribute. Commented May 30, 2012 at 14:11
  • OP posted sets (enclosed in {}). Someone edited it to lists (enclosed in []). Commented May 30, 2012 at 14:13
  • /me steps slowly away from what looks like an edit war in the making.. stets or lists, make up your mind! :-P @eumiro: The OP starts with "I have two lists:". Commented May 30, 2012 at 14:13
  • @MartijnPieters done that was my mistake Commented May 30, 2012 at 14:14

3 Answers 3

6

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']
Sign up to request clarification or add additional context in comments.

Comments

2

Use sets

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

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.