Anshuman Kumar
Anshuman Kumar

Reputation: 13

int() argument must be a string, a bytes-like object or a number, not 'list' Error in code

int() argument must be a string, a bytes-like object or a number, not 'list' errors pops up on the line of code. I am unable to find a solution.

Am I missing something?

L = [[13], [18], [1], [3], [4], [5], [50], [29], [30], [41]]

sum = 10 + int(L[2])

I want sum to be an integer.

Upvotes: 0

Views: 150

Answers (3)

shafikshaon
shafikshaon

Reputation: 6404

This is a list of a list which is 2D. To access an element of the list you need to do something like this

L[2][0] # return 1
L[1][0] # return 18

To solve your problem try this

sum = 10 + int(L[2][0]) # return 11

Upvotes: 0

Mark
Mark

Reputation: 6394

In case you have single elements in your sub-arrays, you can flatten out them into a list without changing further code.

L = [[13], [18], [1], [3], [4], [5], [50], [29], [30], [41]]
L = [i for subarr in L for i in subarr]

sum = 10 + L[2]
print(L, sum)  # => [13, 18, 1, 3, 4, 5, 50, 29, 30, 41] 11

Upvotes: 2

Joe T
Joe T

Reputation: 2350

You have an array of arrays, each with one element.
This would probably work:

sum= 10 + int(L[2][0])  

Or maybe you just want to construct the array without each element wrapped in its own array;

L = [13, 18, 1, 3, 4, 5, 50, 29, 30, 41]

Upvotes: 1

Related Questions