Reputation: 661
I need to remove quotes from second items in nested list. For example, change:
a = [['first', '41'], ['second', '0'], ['third', '12']]
to:
[['first', 41], ['second', 0], ['third', 12]]
I tried
[map(int, [n[1]]) for n in a]
[[41], [0], [12], [0], [45], [17], [3], [10], [1], [19], [98], [0]]
But it removes the first element. Any help is appreciated.
Upvotes: 0
Views: 100
Reputation: 32189
You can do it as:
a = [['first', '41'], ['second', '0'], ['third', '12']]
a = [[i[0], int(i[1])]for i in a]
>>> print a
[['first', 41], ['second', 0], ['third', 12]]
Upvotes: 0
Reputation: 20371
[[item[0], int(item[1])] for item in a]
Output:
[['first', 41], ['second', 0], ['third', 12]]
Upvotes: 2