Python: how to form the correct list


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

Asked By: Tanu



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']]
Answered By: Amin
my_list = [('Australia',), ('Europe',)]
# lambda create a function and returns first value of x
# function map takes a function and list
# it puts every value of list to function 

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

Answered By: burak
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.