Linked Questions
32 questions linked to/from Cost of len() function
9
votes
2
answers
7k
views
What is the big-o notation for the `len()` function in Python? [duplicate]
Possible Duplicate:
Cost of len() function
Does len() iterate over the objects in a list and then return their count? Thus giving it a O(n).
Or....
Does a python list keep a count of any objects ...
13
votes
1
answer
4k
views
Does the len() built-in function iterates through the collection to calculate its length, or does it access a collection's attribute? [duplicate]
Python has many built-in functions, and len() is one of them.
Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or ...
7
votes
4
answers
669
views
Python : How `len()` is executed [duplicate]
Possible Duplicate:
Cost of len() function
How Python calculates length of a list(using len() function )?Does it go through a for or while loop to do the same or it has some internal variable that ...
4
votes
1
answer
5k
views
How is the __len__ method implemented in various container types in Python? [duplicate]
Until now, when I used the len function with various container types (let's say the list type for now), I assumed that each container type has a field member which stores the length of that particular ...
0
votes
0
answers
46
views
Pythonic way to check if two lists are equal in size [duplicate]
Whats the most pythonic or efficent way to check if two lists are equal in size?
The only thing I can think of is
len(list_a) == len(list_b)
but that seems like it would take a lot more processing ...
3222
votes
27
answers
5.4m
views
How do I check if a list is empty?
For example, if passed the following:
a = []
How do I check to see if a is empty?
Ray's user avatar
- 194k
324
votes
6
answers
693k
views
Counting the number of distinct keys in a dictionary in Python
I have a a dictionary mapping keywords to the repetition of the keyword, but I only want a list of distinct words so I wanted to count the number of keywords. Is there a way to count the number of ...
269
votes
5
answers
326k
views
How to assert two list contain the same elements in Python? [duplicate]
When writing test cases, I often need to assert that two list contain the same elements without regard to their order.
I have been doing this by converting the lists to sets.
Is there any simpler ...
72
votes
7
answers
520k
views
Iterating over a 2 dimensional python list [duplicate]
I have created a 2 dimension array like:
rows =3
columns= 2
mylist = [[0 for x in range(columns)] for x in range(rows)]
for i in range(rows):
for j in range(columns):
mylist[i][j] = '%s,%...
17
votes
3
answers
19k
views
Complexity of enumerate
I see a lot of questions about the run-time complexity of python's built in methods, and there are a lot of answers for a lot of the methods (e.g. https://wiki.python.org/moin/TimeComplexity , https://...
23
votes
3
answers
5k
views
Why is Python's 'len' function faster than the __len__ method?
In Python, len is a function to get the length of a collection by calling an object's __len__ method:
def len(x):
return x.__len__()
So I would expect direct call of __len__() to be at least as ...
9
votes
6
answers
1k
views
Python: fastest way of checking if there are more than x files in a folder
I am looking for a very rapid way to check whether a folder contains more than 2 files.
I worry that len(os.listdir('/path/')) > 2 may become very slow if there are a lot of files in /path/, ...
11
votes
2
answers
5k
views
Time Complexity of Python dictionary len() method
Example:
a = {a:'1', b:'2'}
len(a)
What is the time complexity of len(a) ?
8
votes
10
answers
318
views
'in' for two sorted lists with the lowest complexity
I have two sorted lists, e.g.
a = [1, 4, 7, 8]
b = [1, 2, 3, 4, 5, 6]
I want to know for each item in a if it is in b. For the above example, I want to find
a_in_b = [True, True, False, False]
(or ...
11
votes
4
answers
3k
views
perfomance of len(List) vs reading a variable
A similar question has already been ask Cost of len() function here. However, this question looks at the cost of len it self.
Suppose, I have a code that repeats many times len(List), every time is O(...