-1

I need help removing characters from the start and end of every element in a Python list. For instance,

list = ["ab1c", "ef2g", "hi3j"]

If this was the list and I wanted to remove the letters from each element by removing the first two and the last character so that the end result would be list = [1, 2, 3], how could I do this?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Arvo Ju
  • 13
  • 4
  • 1
    Can you share with us what you have tried and where it got `stuck`? – Daniel Hao Jun 16 '22 at 18:22
  • 1
    Welcome to Stack Overflow. Please read [ask]. That this is not a code-writing service. What exactly causes difficulty with this task? For example, what do you imagine are the logical steps to solving the problem, and what parts don't you know how to do? If you had only a single string input instead of a list, would you be able to solve the problem? – Karl Knechtel Jun 16 '22 at 18:25
  • Maybe [Understanding slicing](https://stackoverflow.com/questions/509211/understanding-slicing) answers your question. – wwii Jun 16 '22 at 18:30
  • Please keep in mind that removing the first two characters and the last character from each string **would not** give you a list like `[1, 2, 3]`. It would give you a list like `['1', '2', '3']` - the elements would **still be strings**. – Karl Knechtel Jun 16 '22 at 18:33
  • The question is ambiguous. Do you **always** want to remove the first 2 and last characters? Or are you trying to isolate a number? What if the string isn't long enough - e.g., 'a1b' – DarkKnight Jun 16 '22 at 18:34
  • Please remember to vote on the answers and accept the best solution. See also: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Timur Shtatland Jun 16 '22 at 19:10

4 Answers4

0

Strip first two chars and last char:

vals = ["ab1c", "ef2g", "hi3j"]

out = [val[2:-1] for val in vals]

Gives:

['1', '2', '3']

If you also want as ints (in question you ask for [1, 2, 3] as result) then:

out = [int(val[2:-1]) for val in vals]
Anentropic
  • 32,188
  • 12
  • 99
  • 147
  • 1
    The problem here arises in a case like "abcd1ddd" where there aren't necessarily just 2 characters before and 1 after the number – Blackgaurd Jun 16 '22 at 18:26
  • @Blackgaurd we have no idea what the actual specifications are. Which is exactly why the question should be closed and not answered. – Karl Knechtel Jun 16 '22 at 18:28
  • the question clearly states: _"I wanted to remove the letters from each element by removing the first two and the last character"_ – Anentropic Jun 16 '22 at 18:44
-1

Use re.sub to strip the first two chars and the last one char from the string. Here, . is any character, .* is any character repeated 0 or more times, \\1 is the first capture group, that is, whatever was matched inside the parentheses.

import re
lst = ["ab1c", "ef2g", "hi3j"]
lst = [int(re.sub(r'..(.*).', '\\1', s)) for s in lst]
print(lst)
# [1, 2, 3]
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
-1

You can try using the .strip() method in python. This would work independent of how many characters are before or after the digit in each string.

import string

list = ["ab1c", "ef2g", "hi3j"]
list = [word.strip(string.ascii_letters) for word in list]
Blackgaurd
  • 608
  • 6
  • 15
-1

If the number of characters to be removed is fixed as in the original post, you can do the following:

list = ["ab1c", "ef2g", "hi3j"]
for i in range(len(list)):
    list[i]=int(list[i][2])
# list = [1, 2, 3]