mirko
mirko

Reputation: 21

Delete elements of a List, using a condition

Hello I'm learning to program in Python to manipulate data bases and I can't make this simple task. Please someone help me. I have this list

CIS=['4998200lp','2159140lp','02546or']

I want to get this result:

CIS=['4998200lp','2159140lp','2546or']

I was trying something like:

for x in CIS:
    izq= x[:1]
    if izq == 0:
        CIS=[x.replace(x[:1],'') for x in CIS]
    print (CIS)

I just want to delete the first element of every string for the condition izq == 0.

Upvotes: 1

Views: 78

Answers (4)

MSeifert
MSeifert

Reputation: 152647

Actually your loop contained a very close approach to a working solution:

CIS=['4998200lp','2159140lp','02546or']
CIS=[x.replace(x[:1],'') for x in CIS]

but this would strip all first elements. To only replace them if they are '0' (notice that's not the same as the integer: 0) you need to incorporate you if ... else ... into the list-comprehension:

CIS=['4998200lp','2159140lp','02546or']
CIS=[x.replace(x[:1],'',1) if x[:1] == '0' else x for x in CIS ]

The if ... else ... syntax might be a bit strange but just try to read the code aloud: "Insert the replaced x if the first character is a zero or if not insert x, for every x in CIS".

The other answers contain much more sophisticated approaches but I just wanted to add this answer to give you a heads-up that you were on the right track!

But it's generally a bad idea to use a list-comprehension inside a for loop if they iterate over the same iterable. Mostly you just want one of them.

Upvotes: 0

ShadowRanger
ShadowRanger

Reputation: 155363

Your description doesn't match your example input/output which also differs from your code.

Based on the example input/output, I suspect what you're trying to do is strip a single leading 0 from any string that starts with 0. And that's not too bad, but you can't do it in a for loop without having an index to assign back to. For that, you can use enumerate:

for i, x in enumerate(CIS):
    if x.startswith('0'):  # or x[:1] == '0' if you really prefer
        CIS[i] = x[1:]

Alternatively, you can use a list comprehension to replace CIS:

CIS = [x[1:] if x.startswith('0') else x for x in CIS]

and to mutate in place (rather than making a new list), use the same comprehension but assign to the full slice, which makes it behave like the spelled out loop in the first example:

CIS[:] = [x[1:] if x.startswith('0') else x for x in CIS]

The difference between examples #1/#3 and example #2 occurs if CIS was passed as an argument to a function, or otherwise is referenced in multiple places. In #1/#3, it's mutating the list in place, so all references will see the updates, in #2, it's reassigning CIS, but leaving the original list unchanged; if other references exist, they won't appear changed.

Note: If the goal is to remove all leading 0s, then use str.lstrip, e.g.:

CIS = [x.lstrip('0') for x in CIS]

with similar adaptations for the other approaches. You don't even need to test for the presence of 0 in that case, as lstrip will return the str unmodified if it doesn't begin with 0.

Upvotes: 1

ᴀʀᴍᴀɴ
ᴀʀᴍᴀɴ

Reputation: 4528

Just try to delete first character of every elements that starts with 0:

CIS=['4998200lp','2159140lp','02546or']
for i,v in enumerate(CIS):
    if v.startswith('0'):
        CIS[i] = v[1:]

CIS  # ['4998200lp', '2159140lp', '2546or']

Upvotes: 0

idjaw
idjaw

Reputation: 26578

If you are simply looking to remove the first zero of every string, utilize the startswith method. Also, don't look for an integer 0. Look for a string '0'.

Finally, you can simplify your implementation with doing this all in a comprehension, creating a new list with your new data:

[w[1:] if w.startswith('0') else w for w in CIS]

Outputs:

['4998200lp', '2159140lp', '2546or']

Upvotes: 1

Related Questions