0

I have a list like this:

List= [("Mike,"Admin","26,872"),
       ("John,"Admin","32,872"),
       ("Mark,"Admin","26,232")...]

what I want to achieve is the following:

List= [("Mike,"Admin", 26872),
       ("John,"Admin", 32872),
       ("Mark,"Admin", 26232)...]

so for the every third element I want to remove comma and convert it to int. I tried for example to do replace but the list doesn't support replace method.

Just a test:

    accounts = sorted(List, key=lambda account: account[2], reverse=True)
    for i,j,k in accounts:
         k.replace(",", "")
         int k

Any ideas?

1 Answer 1

1

One way would be:

[(a, b, c.replace(",", "")) for a, b, c in List]

This will create a new list having the first two values unchanged, and the third with the comma being removed.

If you want to have the third value as an int, you can simply.. cast it.

Note that you can iterate on the list in a different way,

[(w[0], w[1], w[2]) for w in List]
Sign up to request clarification or add additional context in comments.

1 Comment

got beaten by a minute :P removing mine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.