Change in place, fast:
The first way:
>>> a=[1,2,3]
>>> B=a
>>> a.extend ([4,5])
>>> A
[1, 2, 3, 4, 5]
>>> b
[1, 2, 3, 4, 5]
The second way:
>>> a=[1,2,3]
>>> B=a
>>> a+=[4,5]
>>> A
[1, 2, 3, 4, 5]
>>> b
[1, 2, 3, 4, 5]
Merge:
>>> A
[1, 2, 3]
>>> B=a
>>> A=a+[4]
>>> A
[1, 2, 3, 4]
>>> b
[1, 2, 3]
For the two ways to traverse the dictionary:
>>> d={' A ': 1, ' B ': 2, ' C ': 3}
>>> for key in D:
... print (key, ' = = ', D[key])
...
b = 2
A = 1
c = 3
>>> list (D.items ())
[(' B ', 2), (' A ', 1), (' C ', 3)]
>>> for (Key,value) in D.items ():
... print (key, ' = = ', value)
...
b = 2
A = 1
c = 3
The For loop generally runs faster than the while counter. Because iterators run at the speed of the C language in Python, while the while loop version runs Python bytecode through a Python virtual machine.
ReadLines will read the file in memory at once, not suitable for large files.
Lists, as well as many other built-in objects, are not their own iterators, because they support opening iterators multiple times. For such an object, we must call ITER to start the iteration:
>>> l=[1,2,3]
>>> ITER (L) is L
False
>>> l.__next__ ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' List ' object has no attribute ' __next__ '
>>> A=iter (L)
>>> a.__next__ ()
1
>>> Next (a)
2
Two types of inverse of list parsing:
Slow:
>>> l=[1,2,3,4,5]
>>> for I in range (len (L)):
... l[i]+=10
...
>>> L
[11, 12, 13, 14, 15]
Fast:
>>> l=[x+10 for x in L]
>>> L
[21, 22, 23, 24, 25]
Python Learning record-1