-3

how can i remove the first 0 from every element in an array. Suppose the array contains [0rf32,0sef2,0fhse] i want it to display [rf32,sef2,fhse] is there a method or function to do that?

azro
  • 53,056
  • 7
  • 34
  • 70
  • What have you tried ? Is that an array of string ? – azro Jul 19 '20 at 11:01
  • 1
    I think you do: `[x[1:] for x in your_array]`, read more about string slicing. – YOLO Jul 19 '20 at 11:02
  • Please take a look at [ask] and how to provide a [mre] to avoid downvotes to your questions in the future. Note that your list is not valid Python syntax and that you are expected to show some efforts in solving your own problem – Tomerikoo Jul 19 '20 at 11:17
  • Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – Roshin Raphel Jul 19 '20 at 11:20
  • 1
    @RoshinRaphel not related to the question. It is about **modifying** elements, not **removing** them... – Tomerikoo Jul 19 '20 at 11:21

3 Answers3

3

You can use slicing notation

values = ['0rf32', '0sef2', '0fhse']
values = [value[1:] for value in values]

To remove the first only if it is a zero do

values = [value.lstrip("0") for value in values]
azro
  • 53,056
  • 7
  • 34
  • 70
0

You can slice every item in the array. In your code, for example, if you want to display 'rf32' from '0rf32' it would be something like:

str = '0rf32'
strResult = str[1:]
print(strResult)

I also think that it can be done with regular expressions (re module on Python). Here you have the documentation: https://docs.python.org/3/library/re.html

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Cristian
  • 36
  • 5
0

You can use a list comprehension and lstrip() to remove zeros from the start of each array value:

array = ['0rf32', '0sef2', '0fhse']
array = [item.lstrip('0') for item in array]
print(array)

Output:

['rf32', 'sef2', 'fhse']
Terry Spotts
  • 3,527
  • 1
  • 8
  • 21