I am trying to get the total no of items in my list which is updated from a SQLAlchemy command, it is a jagged array something like this..
list = [['linux'], ['ML'], ['linux', 'ML', 'Data Science', 'GIT'],['linux', 'Data Science', 'GIT'], ['GIT'], ['ML', 'GIT'], ['linux'], ['linux'], ['linux'], ['Data Science']]
length = len(list)
I get the output as: 10
How do I get the length as: 16 ? that is total no of items in the list ?
Firstly, declare and initialize a jagged array. int[][] arr = new int[][] { new int[] { 0, 0 }, new int[] { 1, 2 }, new int[] { 2, 4 }, new int[] { 3, 6 }, new int[] { 4, 8 } }; Now use the length property to get the length.
Use the len() method to return the length of an array (the number of elements in an array).
A jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These types of arrays are also known as Jagged arrays.
In computer science, a jagged array, also known as a ragged array, is an array of arrays of which the member arrays can be of different lengths, producing rows of jagged edges when visualized as output.
Don't use list
, there's a builtin with that name already. I'll assume you meant "lst".
If your lists are small, use
>>> len(sum(lst, []))
16
Not recommended for large lists because list concatenation with sum
is quadratic in time. However, this is really concise, so reconsider for small inputs.
If your lists are large (or have many elements), use
>>> sum(len(l) for l in lst)
16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With