Python's "itemgetter" is very useful
Вставка
- Опубліковано 28 чер 2024
- Hello everyone (not you Bob)! In today's video we will be learning about itemgetter in Python. It's a really cool function that I just learned about recently, I never really thought about exploring the operator module until this popped up.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels
'I'm going to quickly edit this off screen'
Got a good laugh.
By the time I type itemgetter, I'd have already finished a lambda expression.
Or just use the builtin slice() for ranges
Yeah me too
extractor = lambda items, indexes: [items[i] for i in indexes]
@@BlackneeedWasHere I'd use indices as the plural of index instead
Thanks for this quick overview. Great for working with complex dictionaries. sorted_records = sorted(records key=itemgetter('name, 'occupation')) or powerful sclicing get_fields = itemgetter(0, 5, 7) selected_fields = [get_fields(record) for record in records]
Why not islice?
Wait, no. That’s itertools.
Could we just use lambda expressions? Saves an import line.
Yes
it will probably be slower since it’ll call native bindings at least 3 times (function call, [0] and [n-1]). built in functions *usually* only make 1 native call, making them faster in most cases.
@@itsmaxim01 thanks
@@itsmaxim01 no one cares about speed, unless you code a game in C++
Thanks for showing us this hidden gem, I definitely practice on it.
Immediately useful.
Thanks
Excellent tip!!
Love it, great video thanks
OMG I have been working on an application on using python and I was struggling to get my items this video is a life changer ❤
When I started watching I was about to ask if this works on dictionaries and then once I saw that dict I was overjoyed 🎉🎉😁
Just used it 3 days before for sorting a list of dictionary based on a particular key
I had no idea about slice() either. Thanks
leant something new! keep posting such videos
itemgetter also has a cousin "attrgetter" which grabs attributes instead of items
also you can put slices into itemgetter
Doesn't seem very useful. Takes literally 3 lines of code to implement, doesn't seem worth the overhead of remembering that it exists.
def itemgetter(keys: list[Any]) -> Callable[[Any], list[Any]]:
return lambda obj: [obj[k] for k in keys]
turns out it's two lines including function signature and type hints. In fact writing this adhoc wherever it's used might be clearer.
(I haven't tested this code btw, so if it's wrong lemme know. Also it seems there's a way to encode "an object that can be indexed by a specific type" so those type hints can DEFINITELY be improved)
It could definitely be useful for parsing complex dictionaries.
if it's implemented in operator, it might be compiled into the engine, making this operation blazing fast; if you're doing this op very frequently on a large dataset, that may be one of the optimalisations
in PHP I use array_column a lot
@@pawelabrams It's not, it's implemented in Python
As always all operator methods is useful for custom parsers, not clean production code
Awesome..
In between of video "bob was here" in yellow text, what's that.. ?😅
We can use it in complex json, finally
This could be quite a nice way to validate a json response.
Especially if combined with a TypedDict.
getter=itemgetter(*MyTypedDict.__annotations__.keys())
try:
getter(json_response)
print(“All good”)
except KeyError:
print(“Invalid response”)
I'd probably opt for pydantic whenever it's about input validation.
Built-in data types and built-in functions are same??????
Bob was here!
Hahah I was like, dafuq was that. Rewind and play at 0.25x
If you used item getter on a list of functions isn't that a catalyst for metaprogramming in Python? It seems to me that could choose functions that choose functions infinitely, I think.
Would be neat if python objects accepted tuples in their [] methods like numpy did
And Thank you!!
Hey, can you make a video about seeing what's inside a module's function, I mean actual source code of a module's method. E.g. comb function in math module. By the way, thx for great videos. I've learned a lot from you.
You can type help() in your terminal
Then type math
And you will see the documentation
Import inspect
@@DrDeuteron yes I tried, but it didn’t work for math.comb function
@@codingpointers Yes. However, I don’t wanna see documentation about modules or methods. I wanna see exact source code of comb method not its documentation and I couldn’t figure it out.
@@caesar104 the other option is go to your python distribution and find the code and read it. Just don’t alter it. (My spyder will open numpy source code when I raise an exception inside it……i don’t like it.)
Just use slice data type?
Watch the whole video, and you will learn the difference between both.
What font are you using? I can't seem to find it whatsoever. Looks like JetBrains Mono but for some reason mine looks a little... more edgy
Which IDE are you using?
I think I recognize it as the IDE I use - PyCharm
@@stickmandaninacan ohhh okay thank you!!
I just quickly made this
extractor = lambda items, indexes: [items[i] for i in indexes]
Deletes the need to import so this is a one-liner
I believe it was me who commented something about itemgetter below one of your polls.
Nice video. Can you explain `os.fork()` and `partial()` functions in python next?
Why the returned value is declared as "itemgetter" type instead of tuple or list?
The itemgetter *itself* is an object. A function, if you like.
Once you *apply* it to a list/dict, only then the resulting return value becomes another list/dict.
*hint
Nothing is,declared. But if = itemgetter does have the same information…idk.
@@DrDeuteron What?
@@misamee75 they’re type hints, not type declaration,
X : str = math.sin
Is working code.
@@DrDeuteron, maybe before teaching me the difference between "declaration" and "type-hinting", you may want to work on your use of the English language so we can understand each other.
I was surprised that you didn't also cover `operator.attrgetter()`. Next video?
Potentially!
Doesn't lambda already do this? I will say that this looks a bit cleaner.
practically just a lambda function though isn't it?
Bob was here?!
fr
namedtuple is based on itemgetter
Oh, you just get an error if the key/index doesn't exist? So just define your own getter that let's you specify a default if the key/index doesn't exist. Got it.
there's defaultdict in the collections module
whos bob?
In my personal opinion, using a lambda would be better and less confusing. After all, this is the kind of thing lambdas are designed for.
yes but itemgetter is written in C, much faster execution on large dictionaries...
@@Master_of_Chess_Shorts From a more abstract and less practical point of view, that makes the feature only useful for overcoming the limitations of CPython.
@@tigrankhachatryan6119 ok, hard to tell without specific use cases. I like these quick videos that just cover a simple operator. I also like all the comments it triggers from people like you. ;)
A few people pointed out that they rather use lambdas, but ironically one of the most common places you will see this being used, is in fact, inside a lambda xD
@@Master_of_Chess_Shorts Operator is python file and everything inside of it will run as fast as anything else. Itemgetter is normal python-object.
In this case lambda and other pure-functions most likely are faster, because you don't create object-memory and interface.
.....who's bob?
We don't talk about Bob and his crimes.
Using this certainly violates the KISS principle, makes code both longer and less understandable.
Another way to get the first and last elements of a list or tuple
>>> elements = [1, 2, 3, 4, 5]
>>> elements[0::len(elements)-1]
[1, 5]
Can you please make a video on how to land a job for self-taught programmers.
This is just overcomplicated and is less modifiable.
Just wrote the dam functions. Unless this is faster
f = lambda x: (x[0], x[-1])
Assigning lambdas is considered gauche
@@DrDeuteron it's the most amazing thing
ahahaha
itemgetter = lambda *args: lambda x: tuple(x[i] for i in args) it's basically this
It would be fine if it compiled to the same code, but because it's python - it doesn't
don't call itemgetter a builtin function, that has special meaning in Python
What do you recommend I call it?
@@Indently a standard library function
If just used this code, though I called it heels 👠….like it operatorates on types.LoafType.
My that my nick is Bob ; - (
While I appreciate the usage of type hints to remind people that they are important and should be used, typing local variables like this is just poor form.
I disagree.
it helps with weird cases like the itemgetter, when you don't exactly know what it is. But yeah, in other scenarios, i prefer to use it only for function parameters and return types
Are you really ok with a third of your code being pointless type hinting boilerplate??
Why don't you at least try dropping it for a while? At least until the first time you get your types mixed up when coding for real. You know, forever, because nobody coding dynamically with more than ten minutes experience ever, ever makes that mistake. The whole "type safety" thing is myth.
That game across as grumpy... Sorry! It's early!
What's bad here is not type hinting but the fact that it's completely unnecessary in this case. You don't need to type local variables because the type checker is able to perform local type inference, and you can verify this by hovering over the variable.
Type hinting is mostly useful for function signatures, dataclasses, etc., where it becomes an important tool for defining interfaces. Since you would probably have to describe the types your function accepts and returns in the docstring anyway, you might as well define them in a way that can be checked by automated tools.
> nobody coding dynamically with more than ten minutes experience ever, ever makes that mistake
That is just false. The huge number of "undefined is not a function" you see in the wild from Javascript is enough to disprove that claim. There's a reason static typing came back in full force in recent years, as mainstream languages become better at type inference and reduce the burden of typing (which again, is not what happens in this video).
exactly, the more time I watch your video, the more I find it annoying
@@maleldil1 Sure, there are fringe use cases; it's necessary with dataclasses and if you want to recompile into C for example. And yeah, there a few minor conveniences with IDEs which claw back a tiny fraction of your wasted time.
I'm not sure what js has to do with the price of fish. Ghastly language. Honestly, it's enough to discuss this in python, next thing we'll be bringing up fortran and algol - or, god forbid, haskell - and all hope for humanity will be lost.
Some one told him to "type your code in the video". As he was not sure what they meant, he did both.
Bob was here !