Home > Net >  Python: how to form the correct list
Python: how to form the correct list

Time:11-24

This is my data looks like

my_list = [('Australia',), ('Europe',)]

I need to remove the comma "," after every element.

new_list = [('Australia'), ('Europe')]

I can achieve this using a loop and extracting one element at a time and replacing it. Is there a better way to achieve the same. Thank you

CodePudding user response:

That comma, indicates that you have a tuple. If you want to not have that comma, you can change tuples to lists:

new_list = [list(country) for country in my_list]

It gives You:

[['Australia'], ['Europe']]

CodePudding user response:

my_list = [('Australia',), ('Europe',)]

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

print(country)
  • Related