Array delete

Kent Johnson kent at kentsjohnson.com
Thu Feb 8 16:09:09 EST 2007


azrael wrote:
> if you are new to python then this will be easier to understand. if
> you change this a liitle bit (depending on syntax) it should work in
> any language.
> just copy and paste to a .py file

Yikes. If you are new to Python please ignore this un-Pythonic 
abomination. Check(listItem, temp) is just a long-winded way to say 
'listItem in temp' and the whole thing could better be written as

def main():
     list, temp = [1, 1, 2, 4, 8, 8, 8, 8, 8, 8, 10], []
     for item in list:
         if item not in temp:
             temp.append(item)
     print temp  # print the new list where duplicates are removed

For longer lists the set() method will probably be faster as this one 
searches temp for each item.

Kent

> 
> def main():
>     list, temp = [1, 1, 2, 4, 8, 8, 8, 8, 8, 8, 10], []
>     for i in range(len(list)):
>         if Check(list[i], temp) == 1:
>             temp.append(list[i])
>     print temp  # print the new list where duplicates are removed
> 
> def Check(listItem,temp):
>     for i in range(len(temp)):
>         if listItem == temp[i]:
>             return 0
>     return 1
> 
> if __name__=="__main__":
>     main()



More information about the Python-list mailing list