-3

I want to end up with a list that contains the first elements of a split on all items of a list. That is. input_list = [‘A1 - Some Text’, ‘A2 - Other Text’] should result as output_list = [‘A1’, ‘A2’]. What is the most Pythonic (clever) way of doing this? Extra points for not needing multiple list variables.

UPDATE:

Updating with my initial attempt:

input_list = [‘A1 - Some Text’, ‘A2 - Other Text’]
output_list = []

for element in input_list:
    output_list.append(element.split(' - ')[0]))
adamz88
  • 319
  • 3
  • 12

1 Answers1

0

If l contains your list, I think something like: [i.split('-')[0] for i in l] should provide a good start.

Hans Bouwmeester
  • 1,121
  • 1
  • 17
  • 19