How to concatenate date and time in a list?

Viewed 67

I have a list which looks like this:

my_list = 
[['BCN', '01-01-2021', '08:30:30'],
 ['MCR', '01-01-2021', '12:48:12'],
 ['TNG', '01-01-2021', '10:01:02']]

For every list in my_list iam trying to join index[1] + index[2] and write it back in a new list.

This is my code:

new_list = []
for row in my_list:
    ' '.join(row[1] + row[2])
    new_list.append(row)

print(new_list)

This is my output:

[['BCN', '01-01-2021', '08:30:30'],
 ['MCR', '01-01-2021', '12:48:12'], 
 ['TNG', '01-01-2021', '10:01:02']]

This is my expected output:

[['BCN', '01-01-2021 08:30:30'], 
 ['MCR', '01-01-2021 12:48:12'], 
 ['TNG', '01-01-2021 10:01:02']]
4 Answers

You are just adding the last two elements of each row, not assigning it anywhere. Try this:

new_list = []
for row in my_list:
    new_list.append([row[0], ' '.join(row[1:])])

print(new_list)
# Output
# [['BCN', '01-01-2021 08:30:30'], 
#  ['MCR', '01-01-2021 12:48:12'], 
#  ['TNG', '01-01-2021 10:01:02']]

A shorter version would be:

>>> [[first, ' '.join(rest)] for first, *rest in my_list]

[['BCN', '01-01-2021 08:30:30'],
 ['MCR', '01-01-2021 12:48:12'],
 ['TNG', '01-01-2021 10:01:02']]

EDIT:

>>> my_list

[['BCN', '01-01-2021', '08:30:30', 'other0'],
 ['MCR', '01-01-2021', '12:48:12', 'other1'],
 ['TNG', '01-01-2021', '10:01:02', 'other2']]

>>> new_list = []
>>> for row in my_list:
...     new_list.append([row[0], ' '.join(row[1:3]), row[-1]])

>>> new_list
[['BCN', '01-01-2021 08:30:30', 'other0'],
 ['MCR', '01-01-2021 12:48:12', 'other1'],
 ['TNG', '01-01-2021 10:01:02', 'other2']]

Or,

>>> [[first, ' '.join(rest), last] for first, *rest, last in my_list]

[['BCN', '01-01-2021 08:30:30', 'other0'],
 ['MCR', '01-01-2021 12:48:12', 'other1'],
 ['TNG', '01-01-2021 10:01:02', 'other2']]

For even more items:

>>> new_list = []
>>> for row in my_list:
...     new_list.append([row[0], ' '.join(row[1:3]), *row[3:]])

There is no assignment in this line and it doesn't do what you expect, because it first appends the strings (+) and then places a space between each character (join):

    ' '.join(row[1] + row[2])

This does what you're trying to do:

for row in my_list:
    new_list.append(row[0])
    new_list.append(row[1] + ' ' + row[2])

You didn't change the value of row

Try:

for row in my_list:
    row = [row[0], ' '.join([row[1], row[2]])]
    new_list.append(row)

There is two things you have to consider to fix

  1. You did not assign the new formatted value to any var
  2. + in ' '.join(row[1] + row[2]) first appends then joins

You can fix 2nd issue like

  1. ' '.join([row[1], row[2]])
  2. ' '.join(row[1:])
  3. row[1] + " " + row[2]

So the solution can be like --

# Your list
my_list = [['BCN', '01-01-2021', '08:30:30'],
 ['MCR', '01-01-2021', '12:48:12'],
 ['TNG', '01-01-2021', '10:01:02']]
 
# Newly formatted list
new_list = []
for row in my_list:
    # You have to change these two lines - assign new formatted value to a var and append that into your new_list
    changed = [row[0], row[1] + " " + row[2]]
    new_list.append(changed)

print(new_list)
Related