Home > front end >  Removing PARTS of elements in list - Python
Removing PARTS of elements in list - Python

Time:10-13

my_list = ("12/30/19 14:01", "11/20/19 08:10", "03/13/19 19:30")

Desired output:

new_list = ("12", "11", "03")

I want to remove everything except the month(first two integers).

With only one element in the list, this method works:

new_list = my_list[:-12]

Can anyone please help me?

CodePudding user response:

my_list = list(map(lambda x: x[:2], my_list)) 

CodePudding user response:

You can solve this using python list comp, as follows:

my_list = ("12/30/19 14:01", "11/20/19 08:10", "03/13/19 19:30")
new_list = tuple([x[:2] for x in my_list])

The output of new_list should look like below:

('12', '11', '03')
  • Related