'apply a function on each elements of a list
I want to apply a function to each element of a list. I want to avoid for loop here.
I have list of path names as output from tar.getnames(). I want to get list of filenames. Below is what I tried but I am getting error.
map(os.path.basename(), tar.getnames())
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [8], in <cell line: 1>()
----> 1 map(os.path.basename(), tar.getnames())
TypeError: basename() missing 1 required positional argument: 'p'
Solution 1:[1]
You need to pass a reference to the function os.path.basename
and not what the function os.path.basename
returns when called without arguments ()
:
Use map(os.path.basename, tar.getnames())
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LMD |