I have a list, lst =[['ABC, CN', 'X'], ['DCS, BA', 'X'], ['SCS, TW', 'X'], ['SFA, GW', 'X']]
. Want to remove the last 4 charaters in the in the first part of ever string? Is this possible?
Wanted outcome would be:
newlist = [['ABC', 'X'], ['DCS', 'X'], ['SCS', 'X'], ['SFA', 'X']]
I've tried:
newlist= [sub[:][: -1] for sub in list]
But this gives me:
newlist = [['ABC'], ['DCS'], ['SCS'], ['SFA']]
CodePudding user response:
You can use unpacking when iterate over elements of the list.
>>> [[a[:-4], b] for (a, b) in lst]
[['ABC', 'X'], ['DCS', 'X'], ['SCS', 'X'], ['SFA', 'X']]