why one line "for" expression is faster in Python? [duplicate] - python

This question already has answers here:
Python list comprehension expensive
(1 answer)
What is the advantage of a list comprehension over a for loop?
(1 answer)
Why is local variable access faster than class member access in Python?
(2 answers)
Closed 5 years ago.
I have tested four situations.
1.
testList = list()
for i in range(1000000):
testList.append(i)
2.
testList = list()
for i in range(1000000): testList.append(i)
3.
testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)
4.
[i for i in range(1000000)]
Each time was
1 : 0.09166
2 : 0.08299
3 : 0.05003
4 : 0.04594
Why others are faster than 1?
Can I find related keywords?

Related

Why is the index not being incremented by 2 positions in this for loop? [duplicate]

This question already has answers here:
How to change for-loop iterator variable in the loop in Python?
(6 answers)
Updating Index Value in For Loop
(2 answers)
Closed 1 year ago.
I have a similar code that needs incrementing and while loop cant be used there,
m = range(10)
for i in range(len(m)):
print(i)
i+=2
Do it like this:
m = range(10)
nb = 0
for i in range(len(m)):
print(nb)
nb+=2
The problem is that you wanted to use i for two different tasks.

How to insert a variable in a regex pattern? [duplicate]

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
i have a list :
cpt=0
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*derma.*",l):
cpt=cpt+1
So, instead of putting directly 'derma' as regex , i want :
a= "derma" #initialize
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*a.*",l):
cpt=cpt+1
You can use an f-string:
a= "derma"
list = ["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(f".*{a}.*", l):
cpt += 1

Handling numbers in lists (Python) [duplicate]

This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed 4 years ago.
I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for-loop, how would I set a variable to the sum of the list (15)?
Using a for loop:
acc = 0
for num in list1:
acc += num
Another approach:
acc = sum(list1)

if condition in Python's list comprehension is not working [duplicate]

This question already has answers here:
List comprehension with if statement
(5 answers)
Closed 4 years ago.
I have the following simple code in python giving SyntaxError: invalid syntax
I want a new list with non zero values.
data = [11,2,0,34,8,4]
new_data = [ if x for x in data ]
print( new_data )
Here is how to do this:
new_data = [x for x in data if x != 0]

How to output an index while iterating over an array in python [duplicate]

This question already has answers here:
Accessing the index in 'for' loops
(26 answers)
Closed 8 years ago.
I am iterating over an array in python:
for g in [ games[0:4] ]:
g.output()
Can I also initialise and increment an index in that for loop and pass it to g.output()?
such that g.output(2) results in:
Game 2 - ... stuff relating to the object `g` here.
Like this:
for index, g in enumerate(games[0:4]):
g.output(index)
Use the built-in enumerate method:
for i,a in enumerate(['cat', 'dog']):
print '%s is %d' % (a, i)
# output:
# cat is 0
# dog is 1

Categories