AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Python code10/5/2023 ![]() For example: from itertools import groupby things = for key, group in groupby(things, lambda x: x): for thing in group: print("A %s is a %s." % (thing, key)) print("") In my opinion, this is Itertools’ most useful function. Traditionally, you would use multidimensional loops to achieve this, but in true Pythonic fashion, you can use a function instead. Sometimes when interfacing with external APIs or parsing data, you might need to group a list of items. You can also create permutations: import itertools shapes = itertools.permutations(shapes) 6 - Groupby Output: ('circle', 'triangle') ('circle', 'square') ('triangle', 'square') For example, you can easily create a matrix of all possible combinations and permutations using: import itertools shapes = binations(shapes, 2) Itertools contain many prebuilt methods useful in data analytics or machine learning. The itertools module is a collection of functions that enhance processing of iterators. Output: “” 5 - Combinations and Permutations You can also change the default value to be a null string instead: t = faultdict(str) t Otherwise, you’d be writing more ‘if statements’ than actual code! import collections t = faultdict(int) t This is helpful when you’re analyzing data and need everything to be of a particular data type. With default dictionaries, you can set the default data type for null values. Well, if we had used a default dictionary, that wouldn’t have even been necessary! This has been my savior so many times! In our second point, we saw how to use the “get” function to read dictionary keys safely. This definitely beats looping over and incrementing a counter each time! You can also query it for more information, such as the highest element occurrence: A.most_common(1) However, if you want to group multiple common Lambdas together, they can be stored in dictionaries: lamb = ) For example: square = lambda num: num * num square(5) Normally, you would just store a Lambda function in a variable to be called later. 1 - Lambda Function Dictionariesĭid you know that dictionaries can also store Lambda functions? Lambda functions are those single-line, nameless functions, which can prove quite useful when performing minor alterations to data. You’ll get more mileage out of consistently applying some of them, rather than inconsistently applying all of them. Now, let’s get into the fun bit! It’s time to write better Python code!Īll of the methods outlined below make you a better Python developer, but don’t feel like you have to tackle all of them at once. This article will introduce you to the top 10 ways you might not be fully taking advantage of to write better Python code, including: They might be boring, but they will save you in the long run. Finally, never skimp out on tests and documentation. It’s also a good idea to abide by well-known software principles like DRY ( Don’t Repeat Yourself ) and KISS ( Keep It Simple, Stupid! ). ![]() Style guides help you stick to a format, making it easier to read code written by people on your team – and your own code, as well! I personally prefer “ The Hitchhiker’s Guide To Python. As a precursor to this article, I encourage you to choose a well-accepted style guide if our goal is to write better code. It’s not just other people’s code, though, I sometimes feel like this when I read my own code after a couple of weeks off!įortunately, writing better Python code isn’t hard. This really comes into effect when you’re reading other developers’ code. From dictionaries to collections to itertools to generators, there are lots of ways to write better, more concise Python code, many of which you may be under-utilizing or not even aware of.īut all of these great options mean there might be dozens of ways to write a unit of business logic. The awesome thing about Python is that if you can think of something you’d like to code, there’s probably already a library for it! In fact, Python provides many tools, utilities and other resources to make any coding job easier.
0 Comments
Read More
Leave a Reply. |