0

I have this list lst = ['GS-1', 'GS-2', 'GS-3']

How can I delete the "-" and every thing before it to get something like this? ['1, '2', '3']

Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39

2 Answers2

1

As your list is filled with strings you can use .split() method available to string and loop through the list.

List comprehension way

data = ['GS-1', 'GS-2', 'GS-3']
new = [elem.split("-")[-1] for elem in data]
print(new)
['1', '2', '3']

Normal Way

data = ['GS-1', 'GS-2', 'GS-3']
new = []
for elements in data:
     new.append(elements.split("-")[-1])
print(new)
['1', '2', '3']
1

In a simple one line you can do something like this: numbers = [ item.split("-")[-1] for item in lst] This code iterate every items split it with "-" and takes anything after "-".