Delete Extra Space from Values In List

前端 未结 3 520
礼貌的吻别
礼貌的吻别 2021-01-17 04:37

I have a list of numbers in Python all with a extra space in the front. How do I remove the extra space (so that only the numbers are left)? A short example is below (note

相关标签:
3条回答
  • 2021-01-17 04:58

    For your case, with a list, you can use str.strip()

    l = [x.strip() for x in List]
    

    This will strip both trailing and leading spaces. If you only need to remove leading spaces, go with Alex' solution.

    0 讨论(0)
  • 2021-01-17 04:59
    map(str.strip, List)
    

    or

    map(lambda l: l.strip(), List)
    
    0 讨论(0)
  • 2021-01-17 05:02

    Use str.lstrip here, as the white-space is only at the front:

    List = [s.lstrip() for s in List]
    # ['5432', '23421', '43242', ...]
    

    Or in this case, seeing as you know how many spaces there are you can just do:

    List = [s[1:] for s in List]
    
    0 讨论(0)
提交回复
热议问题