Extending a list in #Python
>>> a = []
>>> a.extend([2, 1]) # neat
>>> a += [3, 4] # hmm ok
>>> a += (7, 11) # huh?
>>> a[-1:] += [18, 29] # uh oh
>>> a[:] = [*a, 47, 76] # 😦
>>> a
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
Extending a list in #Python
>>> a = []
>>> a.extend([2, 1]) # neat
>>> a += [3, 4] # hmm ok
>>> a += (7, 11) # huh?
>>> a[-1:] += [18, 29] # uh oh
>>> a[:] = [*a, 47, 76] # 😦
>>> a
[2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
@peterdrake Exactly what I had in mind when sharing that. 😜
Though in this case I'd say there's approximately two obvious ways to do it.
a.extend([2, 1])
And
a += [2, 1]
Which I use depends on what the surrounding code looks like and the whim of the moment. 🤷
true, but only for lists!
>>> a = [1, 2]
>>> b = (3, 4)
>>> a += b
>>> b += a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple
The list += operator was made consistent with the list extend method.
But other sequences, both mutable and immutable, typically allow only the same type with +=.
I explain it in my Python Oddities talk here: https://youtu.be/nWC73Llo170?t=735
@b11c believe it or not, all of them, the last one included, actually mutate the original list.
Even += mutates the list. For lists, += is pretty much syntactic sugar for extend.
@talideon yup. that's a weird one!
I explain it in my Python Oddities talk here: https://youtu.be/nWC73Llo170?t=735
The short explanation is that the += operator on lists accepts any iterable to make it consistent with the list extend method.
@treyhunner
>>> a[:] = [*a, 47, 76] # 😦
IIRC found via StackOverflow about "subdir_list[:] = []", as "os.walk()" https://docs.python.org/3/library/os.html#os.walk lacks a variable to control recursion:
import os, random
parent_dir = '.'
recurse = random.randint( 0, 1 )
print( f'{recurse = } in {parent_dir}' )
for cur_dir, subdir_list, _ in os.walk( parent_dir ):
if subdir_list:
if not recurse:
subdir_list[:] = []
subdir_list.sort()
print( [ f'{cur_dir}/{s}' for s in subdir_list ] )
#Python #wart
...
Use of "os.listdir()" https://docs.python.org/3/library/os.html#os.listdir is not interchangeable with "os.walk()" as the former does not recurse by itself & the return values are different.
Else, would have just switched the functions:
walker = os.walk if recurse else os.listdir
# Not interested in
# - checking the type or number of elements for the loop variable on each iteration;
# - needing to generate paths correspondingly.
for <?> in walker():
...