DEV Community

Cover image for 71 Python Code Snippets for Everyday Problems

71 Python Code Snippets for Everyday Problems

Jeremy Grifski on January 29, 2020

If this is your first time here, I'd like to encourage you to head over to the original post which is regularly maintained. At this point, it feat...
Collapse
 
michelleanaya profile image
michelleanaya

I saved your article and python code. Maybe someday come in handy. I would also be interested to find information on python libraries.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Are there any libraries you’re interested in or would you want a general list?

Collapse
 
frosty110 profile image
Blaise D. Albuquerque

I think a general list of everyday libraries, what you use it for, and common methods would be interesting to see.

This is a great list, thank you! It's definitely super helpful for someone like me who's new to python.

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

This is a great idea! I don't use a ton of libraries outside of what's already a part of the standard library (e.g. csv, sys, os, etc.), but I could probably piece together a similar article to this one.

Collapse
 
leahein profile image
Leah Einhorn

Nifty list!

Another way to clone a list:

my_duplicate_list = [*my_list]
Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Oh, this is cool! I can't believe there are even more slick ways to copy a list. Do you mind if I include this in the original article with credit to you for the tip?

Collapse
 
leahein profile image
Leah Einhorn

Absolutely, happy to contribute :)

Thread Thread
 
renegadecoder94 profile image
Jeremy Grifski

Just updated the article to include your solution. Did you know this solution is blazing fast?!

Collapse
 
richardcochrane profile image
Richard Cochrane

Super helpful list especially helping remind more experienced hands that your favourite approach is not necessarily the latest or preferred approach. Especially look forward to playing with f-strings when we do our Python 3 upgrade. Just one typo - in the last code block "import cProfile" is included in the comment rather than on it's own line.

Thanks again Jeremy!

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Thanks for the heads up, Richard! When I ported this over using RSS, the code blocks were imported as a single line, so I had to manually reformat them. I wouldn't be surprised if there were more artifacts like that.

Also, great call on the f-strings. I've been addicted to them for a little while now! Very convenient.

Collapse
 
bngcebetsha profile image
Buntu Ngcebetsha

where's the other like button so I can smash it many times :)

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Haha glad you like it. Unless this is a clever way of hinting at a dislike button. In that case, you win this time!

Collapse
 
bngcebetsha profile image
Buntu Ngcebetsha

Full proof reply, I've book marked the article and the reply for laughs later on in life...

Collapse
 
wrldwzrd89 profile image
Eric Ahnell

Very nice, covering a bunch of common problems Python programmers like myself face. I didn't know about all those quirks though.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I’m a bit of a language feature nut! Glad you liked the list.

Collapse
 
hasii2011 profile image
Humberto A Sanchez II

Don't forget f-string formatting

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

I believe it’s there! Should be the last one in the string formatting list.

Collapse
 
yokotobe profile image
yokotobe

Helpful one

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Glad to hear it!

Collapse
 
iceorfiresite profile image
Ice or Fire

Awesome list @renegadecoder94

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Hope it’s helpful!

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Woah! That's a great list! 🤩

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

Thanks for the kind words!

Collapse
 
ckesanapalli profile image
Chaitanya Kesanapalli

Thanks a lot for share these snippets.

In future can you also share snippets of logging, expection handling and main libraries like numpy arrays and pandas dataframes.

Collapse
 
zdev1official profile image
ZDev1Official

Bro that's good!
Solves %85 of my problems!

Collapse
 
sivanesh121 profile image
sivanesh121

super

Collapse
 
christianbos91 profile image
Christian Buendia

Awesome! Thanks for your time

Collapse
 
codeperfectplus profile image
Deepak Raj

Website to Learn Python, Machine Learning And Data Science.
bit.ly/codeperfectplus

Collapse
 
petermortensen profile image
Peter Mortensen

How did that add up to 71? I counted 16.

Collapse
 
renegadecoder94 profile image
Jeremy Grifski

71 refers to the number of code snippets (which could be wrong), not the number of problems.

Collapse
 
digital_hub profile image
hub • Edited

This is a great list, thank you! It's definitely super helpful for someone like me who's new to python.

plz do me a favor - puplish this on gitub - this is the place for sharing such stuff

Collapse
 
manishanaidu profile image
Manisha Naidu

How to delete a value using an index in set? I am aware that set does not allow indexing.