0

I have this list in a list

a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

but i need it to be ints , im not sure where to use int() to change str to int

a = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]

4 Answers 4

3

You can use a nested list comprehension like so:

a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]
b = [ [int(j) for j in i] for i in a]
Sign up to request clarification or add additional context in comments.

Comments

1
In [51]: a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

In [52]: [map(int, l) for l in a]
Out[52]: [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Comments

0

An example using nested list comprehension:

In [1]: a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]

In [2]: [[int(s) for s in l] for l in a]
Out[2]: [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Comments

0

Here's an idea:

>>> a = [['1','2','3','4'],['1','2','3','4'],['1','2','3','4']]
>>> map(lambda l: map(int, l), a)
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.