I have a list for exemple:
a = [ 9,6,5,4,1,0,0,7,6] .
By this function I want to delete all element which content zero and delete all element which content duplicate.
But it doesn’t do exact what I want.

def rensa(a,b):
    j=0
    for i in a:
        if a[j]<= 0:
            del a[j]
        elif a[j]==a[j+1]:
            del a[j+1]
        j+=1
        print a
    return a,b
Member Avatar for kdoiron

The easiest way would probably be to create a new list ('b', perhaps? You've defined 'b' in your function but never use it). Then copy each item from 'a' to 'b' unless it is 0 or already in 'b'.

How do you mean when I copy from a to b delete zero and duplicate?

Member Avatar for kdoiron

Create a new list called 'b'. Then pass through list 'a', and check each element against list 'b'. Use the 'append' method to add to list 'b' if you need to.

And for what it's worth, I probably would use more meaningful variable names than 'a' and 'b'.
Here's some code.

old_list = [ 9,6,5,4,1,0,0,7,6]
new_list = []
for item in old_list:
    if item != 0:
        if item not in new_list:
            new_list.append(item)
print old_list
print new_list

Output:
[9, 6, 5, 4, 1, 0, 0, 7, 6]
[9, 6, 5, 4, 1, 7]

Thank you very much for your help, but I work with the same list many time if there are more zero and duplicate the program must delete them.
I mean for example if I have a list 4.4.2 the program take minus one from every element then put it in a new element like that.
4.4.2
3.3.1.3
2.2.0.2.4
1.1.0.1.3.4
0.0.0.0.2.3.4
0.0.0.0.1.2.3.4

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.