Linked Questions
29 questions linked to/from How do I get the number of elements in a list (length of a list) in Python?
762 votes
8 answers
1.7m views
Is arr.__len__() the preferred way to get the length of an array in Python? [duplicate]
In Python, is the following the only way to get the number of elements? arr.__len__() If so, why the strange syntax?
300 votes
7 answers
1.1m views
Python - Count elements in list [duplicate]
I am trying to find a simple way of getting a count of the number of elements in a list: MyList = ["a", "b", "c"] I want to know there are 3 elements in this list.
208 votes
5 answers
830k views
Counting array elements in Python [duplicate]
How can I count the number of elements in an array, because contrary to logic array.count(string) does not count all the elements in the array, it just searches for the number of occurrences of string....
34 votes
3 answers
97k views
Python test Average Calculator returen error 'list' object has no attribute 'len' [duplicate]
Hey this is a demo to show some of my classmates an intro to python and coding. The code below should be able to take a list like [0,1] and if run using the average function would return 0.5. When run ...
10 votes
1 answer
15k views
Python 2.7, How can I get/check the size of a list? [duplicate]
I've got an array (list) that I want to check if it's size is equal to 1, if it is then it needs to append a new like as shown. ## If appended data = 1 then append the new line: if appended_data == 1:...
7 votes
2 answers
2k views
Return different value depending on number of arguments in a function [duplicate]
Im trying to make a function that takes from 1 up to 5 arguments and does different calculations depending on the number given. My idea was something like this: def function(*args) num_of_args = (...
-2 votes
1 answer
14k views
count number of lists inside a list python [duplicate]
I have a list like this : test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0,...
-3 votes
1 answer
2k views
Count Amount of Values in a List [duplicate]
Possible Duplicate: Get the size of a list in python? I need to be able to count how many values there are in total there are in a list. eg. [1,5,9,4,6], there are 5 values. I have tried using the ...
0 votes
4 answers
1k views
How to count elements in nested lists [duplicate]
How do I count the number of elements in a nested list? I want to transform: myList = [[0],[1,4,5,8],[5],[2,3,9],[7,7]] into: myList = [1, 4, 1, 3, 2]
0 votes
2 answers
2k views
Using 'for loop' to sum nested list values and return (total of sum) [duplicate]
I am receiving the count of each line in each list, I am looking to sum each particular values of entire list, (Nested lists included) [[3],[4],][1],[3]] = * 11 is my desired result. example: code ...
0 votes
1 answer
664 views
Why can't I use the count method of list? [duplicate]
I use the method split() of the string class and supposedly it returns a list, but it isn't a common list because I can't use list methods like count() I've tried to use the count method of list but ...
-1 votes
1 answer
408 views
How do i find number of objects in list in Python [duplicate]
For example, list[1, 2, 3] has 3 items (and indexex 0, 1, 2). How can I print the number of objects in a list? In this istance, it would be "3".
-1 votes
1 answer
232 views
How to count number of id's in a sqlalchemy database using query.all()? [duplicate]
class Artist(db.Model): id = db.Column(db.Integer(), primary_key=True) artist_name = db.Column(db.String(200)) albums = db.relationship('Album', backref='artist'...
1 vote
0 answers
47 views
How do I count the number of features detected through MSER? [duplicate]
I am trying to count the number of features detected by MSER feature detection algorithm in OPenCV Python. I have no idea how to do that. I would greatly appreciate it if someone helped me figure out ...
-3 votes
2 answers
67 views
What can I do if I want to turn the second element in a sublist into a float, but not every list in the list has a second element? Python [duplicate]
Here is an example of a list of lists. [["Amber", "7.3"], ["Molly", "14.5"], ["Lisa"]] As you can see, there are two lists that have a second element....