Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:19:44+00:00 2026-05-15T10:19:44+00:00

How do I remove a character from an element in a list? Example: mylist

  • 0

How do I remove a character from an element in a list?

Example:

mylist = ['12:01', '12:02']

I want to remove the colon from the time stamps in a file, so I can more easily convert them to a 24hour time. Right now I am trying to loop over the elements in the list and search for the one’s containing a colon and doing a substitute.

for num in mylist:
    re.sub(':', '', num)

But that doesn’t seem to work.

Help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:19:45+00:00Added an answer on May 15, 2026 at 10:19 am

    The list comprehension solution is the most Pythonic one, but, there’s an important twist:

    mylist[:] = [s.replace(':', '') for s in mylist]
    

    If you assign to mylist, the barename, as in the other answer, rather than to mylist[:], the “whole-list slice”, as I recommend, you’re really doing something very different than “replacing entries in the list“: you’re making a new list and just rebinding the barename that you were previously using to refer to the old list.

    If that old list is being referred to by multiple names (including entries in containers), this rebinding doesn’t affect any of those: for example, if you have a function which takes mylist as an argument, the barename assignment has any effect only locally to the function, and doesn’t alter what the caller sees as the list’s contents.

    Assigning to the whole-list slice, mylist[:] = ..., alters the list object rather than mucking around with switching barenames’ bindings — now that list is truly altered and, no matter how it’s referred to, the new value is what’s seen. For example, if you have a function which takes mylist as an argument, the whole-list slice assignment alters what the caller sees as the list’s contents.

    The key thing is knowing exactly what effect you’re after — most commonly you’ll want to alter the list object, so, if one has to guess, whole-list slice assignment is usually the best guess to take;-). Performance-wise, it makes no difference either way (except that the barename assignment, if it keeps both old and new list objects around, will take up more memory for whatever lapse of time both objects are still around, of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I remove duplicate characters from a string using Python? For example, let's
I want to remove n characters from each line using PERL. For example, I
I want to remove first character from link's text with jQuery. <span class=test1> +123.23
I want to remove the '\' character from my strings. I've tried several ways
I want to remove a character from a string in permutation.... Let us say
Can anyone please help me? I need to remove the first character from a
I want to remove a character ('.') from a string without knowing its position.
I'm attempting to remove a character from a string in C. The problem I
I am trying to remove the last character from a string, if it is
I am trying to remove the very last character from a TextCtrl object in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.