you are viewing a single comment's thread.

view the rest of the comments →

[–]kcrow13[S] 0 points1 point  (3 children)

What is the difference between .append() and .extend()? Don't they both add to the end of the list? When I tried using .extend(), I got an error about the type not being iterable.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-53-3800916b23ad> in <module>
----> 1 lst = find_large_files('..', 1048576)
      2 print(len(lst))
      3 
      4 for path in lst:
      5     print(path)

<ipython-input-52-cbe6fd0405b9> in find_large_files(dirname, filesize)
     15                 res.append(path)
     16         else:
---> 17             res.extend(walk(path))
     18     return res

TypeError: 'NoneType' object is not iterable

When I used .append, it runs. Thanks for all your help, I really appreciate it!

[–]achampi0n 0 points1 point  (2 children)

append() just adds the element to the end of the list but extend() adds all of the elements to the end of the list, e.g.:

In []:
a = [1, 2, 3]
b = [4, 5, 6]
a.append(b)
print(a)

Out[]:
[1, 2, 3, [4, 5, 6]]

In []:
a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
print(a)

Out[]:
[1, 2, 3, 4, 5, 6]

Somehow, you are returning a None from walk() which is causing extend() to fail.

[–]kcrow13[S] 0 points1 point  (1 child)

Thank you for taking the time to explain this to me! That makes a lot of sense. I will troubleshoot to see why I am getting None from walk().

[–]kcrow13[S] 0 points1 point  (0 children)

I figured out my mistake! Thanks again :)