r/learnprogramming • u/WGBtom • Feb 27 '24
Python In your opinion, what is the most underated python function?
Preferably a niche function but a common one is fine as well if you think it is underated, all answers are welcome! I just started learning python on Codecademy so I would love to hear what answers you guys have so that I can check each of them out and experiment with them :) For myself, I just learnt about zip() and I love it, i think it's really helpful, just a shame that the data points it combines are stored as tuples.
36
Upvotes
•
u/AutoModerator Feb 27 '24
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
9
u/Irish_beast Feb 27 '24
This is so so untrue. The inputs can be an iterable, and the zip itself is an iterable
This creative but pointless snippet I just wrote has not a tuple in sight
import builtinsimport keywordzipped = zip(map(str, dir(builtins)), map(str, keyword.kwlist))print(zipped, type(zipped))for b, k in zipped:print(b, k)
13
u/drbobb Feb 27 '24
Agreed about zip(), but I don't really understand your final remark. What's wrong with tuples?
Also, enumerate(). Never write
for i in range(len(foo))again. It's ugly and unnecessary.99
u/hp_sauce_ Feb 27 '24
Not really a function, but f-strings are great for formatting. I use them all the time
29
u/pancakeQueue Feb 27 '24
Did you know that python has built in documentation, well if it’s written that is. Use the function help() on any other function. So like help(print) will give you docs on the print function.
I hate it cause I’d rather have C style man pages but it still good to know.
79
u/eruciform Feb 27 '24
print()
Gotta give some love for the poor man's debugger, it's not just for output data!
Otherwise maybe zip() (as you said) or reduce()
A lot of people don't use yield
33
u/tiredITguy42 Feb 27 '24
List comprehension and combining them with if statements. So many people are using regular cycles, but if used right these are easier to read and faster to execute.
8
u/[deleted] Feb 27 '24
main()
7
u/Best_Recover3367 Feb 27 '24
partial from functools, basically let you turn your function into a callback, which means you get to use your function now as an argument to pass into other function that you cannot otherwise pass into without giving the it some arguments first
10
u/likethevegetable Feb 27 '24
Not exclusive to python, but I fucking love making lists of strings like this:
'Apple Orange Pear'.split()Wanting to hear your other little hacks like this.
13
u/[deleted] Feb 27 '24
eval() and exec(): execute the strings passed to them as Python code. They are very powerful for dynamic execution of Python code but can be a security risk if the input is not properly sanitized.
3
u/iamevpo Feb 27 '24
Itertools and Enum do not get enough spotlight
3
u/dparks71 Feb 28 '24
Not a function, but any object +
.__dict__because it hasn't been mentioned yet. Dumps every available attribute of the class so if there's something you know exists but you can't remember the name of it you can see it quickly.2
u/Specialist-Ad7393 Feb 27 '24
For me it's dir() and help(). I know it sounds basic but when I don't know how to work with a library it helps out
2
u/[deleted] Feb 27 '24
Not part of the standard library, but pandas.DataFrame.groupby is one of the things I wish I had found earlier.
2
u/captainAwesomePants Feb 28 '24
I think it's functools.cmp_to_key.
Sorting with Python is a bit tougher than other languages because the sort functions want a "key" for each value, and not a function that compares two values. This can make things annoying:
Enter functools.cmp_to_key:
Works perfectly, and you'd never know it was an option if you just looked at the parameters on sort functions.
2
u/Xarlyle0 Feb 28 '24
Generators. Awesome to chain together or use inside function calls.
1
u/CamilorozoCADC Feb 28 '24
Not exactly a function but the entire pathlib module is a blessing for filesystem related stuff.
I cannot understand why some people insist on going the os.path way which tends to fall apart when switching OSes
1
u/Kyouma118 Feb 28 '24
python -m http.server 8000for insta-server in the same directory :)1
u/[deleted] Feb 28 '24
glob()is a staple for me! Even betterpathlib'sPathclass is a wonder.0
u/[deleted] Feb 28 '24
[deleted] — view deleted comment