I was trying to do some simple procedures using lists. From the book learning python I saw the method of using a comprehension. Well I also knew that a loop could replace it. Now I really want to know that which is faster, loop or comprehension. These are my programs.
a = []
for x in range(1, 101):
a.append(x)
This would set a
as [1, 2, 3, ......, 99, 100]
Now this is what I have done with the comprehension.
[x ** 2 for x in a]
This is what I did with the loop.
c = []
for x in a:
b=[x**2]
c+=b
Could any one say a way to find which of the above is faster.Please also try to explain that how comprehensions differ from loops. Any help is appreciated.