Suppose I have the below list and I want to be able to join the last three items in each list and put the joined items back to the original list. How would i go about doing that using something like list comp? I know how to do it by writing an elaborate lines of codes, however what I'm looking for is an efficient one or two liner code that will accomplish what I want to do.
My list:
lines = [
['0', 'AG1C', 'A', '05/27/2020', '10:20', 'PM'],
['1', 'AG1C', 'B', '05/27/2020', '1:22', 'PM'],
['2', 'AG1C', 'B', '05/16/2020', '12:58', 'AM'],
['3', 'AG1C', 'C', '05/17/2020', '10:34', 'AM'],
['4', 'AG1C', 'C', '05/28/2020', '2:10', 'AM']]
Here's what I tried and didn't work:
lines = [line[:2].append(f'{line[-2]} {line[-1]}') for line in lines]
My desired output:
print(lines)
[['0', 'AG1C', 'A', '05/27/2020 10:20 PM'],
['1', 'AG1C', 'B', '05/27/2020 1:22 PM'],
['2', 'AG1C', 'B', '05/16/2020 12:58 AM'],
['3', 'AG1C', 'C', '05/17/2020 10:34 AM'],
['4', 'AG1C', 'C', '05/28/2020 2:10 AM']]
Any help will be much appreciated
CodePudding user response:
Maybe something like this can you help:
x = [[line[0], line[1], line[2], ' '.join(line[3:])] for line in lines]
print(x)
Output:
[['0', 'AG1C', 'A', '05/27/2020 10:20 PM'],
['1', 'AG1C', 'B', '05/27/2020 1:22 PM'],
['2', 'AG1C', 'B', '05/16/2020 12:58 AM'],
['3', 'AG1C', 'C', '05/17/2020 10:34 AM'],
['4', 'AG1C', 'C', '05/28/2020 2:10 AM']]
CodePudding user response:
You were actually really close to the correct answer. The problem in your solution is that .append does not return the actual list, it just modifies the list in place and returns None. For that you can sum 2 lists which will return another list like this:
lines = [line[:2] [f"{line[-2]} {line[-1]}"] for line in lines]
Now the problem is that the items with index 2 and 3 are being ignored
lines = [line[:3] [f"{line[-3]} {line[-2]} {line[-1]}"] for line in lines]
This returns the desired output, but there are still some things to change.
Also that f-string doesn't look "pythonic". Another way to do that would be to get a list of the last 3 elements and use the string join method like this:
lines = [line[:3] [" ".join(line[-3:])] for line in lines]
This only works for this specific case where the original lists have length 6.
lines = [line[:-3] [" ".join(line[-3:])] for line in lines]
Using line[:-3]
will get all the elements except for the last 3. This is the general solution.
CodePudding user response:
Here's yet another way to do this:
lines = [line[:3] [' '.join(line[3:])] for line in lines]
CodePudding user response:
Another readable approach without slicing:
lines = [[a, b, c, " ".join(d)] for a, b, c, *d in lines]
CodePudding user response:
lines = [
['0', 'AG1C', 'A', '05/27/2020', '10:20', 'PM'],
['1', 'AG1C', 'B', '05/27/2020', '1:22', 'PM'],
['2', 'AG1C', 'B', '05/16/2020', '12:58', 'AM'],
['3', 'AG1C', 'C', '05/17/2020', '10:34', 'AM'],
['4', 'AG1C', 'C', '05/28/2020', '2:10', 'AM']]
for i in lines:
last_items = ' '.join(i[3:len(i)])
for k in range(3):
i.remove(i[-1])
i.append(last_items)
print(lines)
CodePudding user response:
One line of code:
for i in range(len(lines)): lines[i][3:] = [' '.join(lines[i][3:])]
Output:
[['0', 'AG1C', 'A', '05/27/2020 10:20 PM'],
['1', 'AG1C', 'B', '05/27/2020 1:22 PM'],
['2', 'AG1C', 'B', '05/16/2020 12:58 AM'],
['3', 'AG1C', 'C', '05/17/2020 10:34 AM'],
['4', 'AG1C', 'C', '05/28/2020 2:10 AM']]