Reputation: 37
I am trying to find if a value already exists in a list or not,below is my code, and expected output, can someone correct me where i went wrong
isid_node=["100","105"]
isid_init_val=100
isid_new=[]
while(len(isid_new)<=5):
if isid_init_val in isid_node:
isid_init_val=isid_init_val+1
else:
isid_new.insert(20,isid_init_val)
isid_init_val=isid_init_val+1
print isid_new
i am getting [100,101,102,103,104,105]
i am expecting [101,102,103,104,106,107]
please can you correct my code??
Upvotes: 0
Views: 26
Reputation: 2457
You've made a small mistake, the string "100"
will not be equivalent to 100
(the number), which is essentially what you are checking for.
If you change you initial list to isid_node=[100,105]
instead of isid_node=["100","105"]
(what you currently have), you should get your expected output.
Upvotes: 1
Reputation: 603
You're comparing strings and numbers. Even if the string is a string representation of the number, they're not equal. Either you need to store numbers as numbers of you need to test it the string representation of your number is in the list.
For example :
isid_node=[100,105]
isid_init_val=100
isid_new=[]
while(len(isid_new)<=5):
if isid_init_val in isid_node:
isid_init_val=isid_init_val+1
else:
isid_new.insert(20,isid_init_val)
isid_init_val=isid_init_val+1
print isid_new
Upvotes: 1