Hy guys!
Today I'm going to share with you the best tips and tricks to master in Python!
These tips are based on my experience on Codingame, during ClashOfCode (I was in the top 100 at one time :) )
1/ Create a number sequence
Sometimes you may want to create a sequence of numbers: a fairly intuitive way would be to create a loop and perform n calls of the append() method.
my_list = []
for i in range(0, 10, 1):
my_list.append(i)
In reality this operation is quite time consuming, and it is better to write :
my_list = list(range(0,10,1))
>>> my_list = [0,1,2,3,4,5,6,7,8,9]
This is not only faster to write but also faster to compute!
2/ Overlaying two dictionaries
Concatenating two dictionaries can be a useful operation to group information and avoid getting lost in lots of variables...
So we can use the update() method:
a = {“alpha”: 3, “beta”: 5}
b = {“gamma”: 1, “delta”: 12}
a.update(b)
>>> a = {“alpha”: 3, “beta”: 5, “gamma”: 1, “delta”: 12}
Warning: if there are two identical keys, then the value will be that of dictionary b.
3/ Create a dictionary of a sequence
By creating a dictionary of a sequence, I mean easily creating (in one line) a dictionary where the key depends on x and where the value also depends on x. However, this method can be modified to create the dictionary according to a list, inputs, etc...
my_dic = {(x+2): (x**2 + 1) for x in range(4)}
>>> my_dic = {2: 1, 3: 2, 4: 5, 5: 10}
4/ Reverse a list
Inverting a list is one of the most useful things you can do in Python. You must know this operation !
my_list = [1, 2, 3, 4]
my_list = my_list[::-1]
>>> my_list = [4, 3, 2, 1]
It is much faster to write, much more readable, and especially much faster to execute than the built-in function reversed().
5/ Unpacking a tuple
Unpacking a tuple is an interesting operation especially to perform operations on the values, and avoid having to retrieve the value by its index each time:
my_tup = (0, 1, 2, 3)
a, b, c, d = my_tup
>>> a = 0
>>> b = 1
>>> c = 2
>>> d = 3
6/ Filter a list
Filtering a list is a useful process in algorithms or in more common programs. You can keep the values you want by passing a list into a function that acts as a filter. This function returns True (the value is kept) or False (the value is deleted).
my_list = [1,2,3,4]
def my_filter(x):
if x==3 or x%2==0:
return True
else:
return False
my_list = list(filter(my_filter, my_list))
>>> my_list = [2,3,4]
7/ Return multiple values from a function
We know that to return a value from a list, we must use return. However, the function stops after returning a value. However, we can use yield to continue to execute the function. Useful for returning variables, for debugging, etc.
def my_func(x)
for i in range(x):
yield x**2
for k in my_func(4):
print(k)
>>> 0
>>> 1
>>> 4
>>> 9
8/ import this
The this library is more a joke than a trick, but it's nice to know the little easter eggs of its language:
import this
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
. . .
9/ Basic operations on sets
If we have two sets a and b such that :
a = {1,2,3}
b = {3,4,5}
then we can perform the following operations that you must know !
# Union
print(a & b)
>>> {1, 2, 3, 4, 5}
# Intersection
print(a | b)
>>> {3}
# Symetric difference
print(a ^ b)
>>> {1, 2, 4, 5}
10/ Code if : else : in one line
It's not the most useful trick, but it's always useful to know how to write an if : else : in one line to make the code cleaner, or in shortest code competitions on codingame !
i = 1 if True else 2
>>> i = 1
11/ Limit the number of recursion of an algorithm
Limiting the depth of recursion of an algorithm is important. It is even the first thing to do when you know the maximum depth of recursion! You can do it with the Python library sys:
import sys
sys.setrecursionlimit(1000)
12/ Print text faster
When we have to print text in Python, we use by default the print() method ! However, when you have to print thousands of lines, this method can be slow. In this case we use :
import sys
sys.stdout.write(…) # only string
You can use a similar method for the input, but it's a bit more complex :)
This method is up to 8 times faster than the normal print
13/ Have the middle items of a list
Having the items in the middle of a list is a little trick to know when unpacking this list... Indeed, depending on the number of variables (or underscore if you don't want to keep the variable) at the beginning and at the end, you can have a variable containing a list of values containing only the "middle" one, in fact, the one that have not been put in other variables!
l = [1,2,3,4,5,6,7]
a, *b, c = l
>>> a = 1
>>> b = [2,3,4,5,6]
>>> c = 7
14/ Separate big numbers by _
For more readability, and because Python does not allow spaces between the digits of a number, we can use _. For example, 1 000 000 000 can be written in Python :
1_000_000_000
15/ Exchange keys / values of a dictionary
Exchanging the keys and values of a dictionary is a technique that can be useful, especially in the field of AI :). Here is how to do it:
my_dic = {“a”: 1, “b”: 2}
my_dic = {v:k for k, v in my_dic.items()}
my_dic = {1: “a”, 2: “b”}
Conclusion
That's all for today, I hope you liked the article and that you were able to improve your coding skills! Don't hesitate to share the article with your friends and to give me your feedback in comments!
Adrien
Discussion (20)
I see myself coming to this article again and again in the future
Dear friend,
Your website does not have three links working, as far as I can see.
Yeah !!!
Because it is in under-construction....
Hey are you on in instagram follow my id is zastixx
Thank you very much !
A neat trick with sets
will give you
There is also the reverse() method which reverses the list in-place, while [::-1] gives a new list in reversed order.
gives you
Happy Coding
Yes! For the first trick, it is a very cool property of the set function to get unique values.
For the second trick, I explained in my post that this built-in function is much slower, but it is probably easier to remember, I admit :)
nice I didn't know some of that, like the
a, *b, c = l
thing andimport this
lol very coolJust a quick note (that can count as another trick): you can simplify the
my_filter
function to thereturn x==3 or x%2==0
It's true that you can use that, but I wanted to keep it pretty clear in my post. But your trick is useful in "small" functions like this one to avoid making the code ugly
Horrible. Irrelevant, outdated, misleading, and sometimes even plain wrong.
Very nice description of your comment ;)
By the way, can you tell me where my mistakes are? When you say it's irrelevant and outdated, I want to point out that this post is for beginners and that these are tricks I see regularly on CodinGame.
You say that my post is misleading. As far as I know, the title corresponds perfectly to what I do: I present 15 techniques that beginners should know, not to use in all cases, but that can be interesting in some competitions, or in projects. Also, if you were expecting to see tricks that nobody knows, you won't find them here, since it is for beginners.
Since your comment is very poorly constructed and is very nasty, I'll report it to dev.to!
Have a nice day, Adrien
Though terse, my comment was justified. I have no problem explaining, but you must understand that I encounter heaps of SIWOTIes (urbandictionary.com/define.php?ter...) and many times people never read my comments. So I just post something terse to provoke a reaction, to see if there is someone who wants to learn on the other side. You replied, so here goes.
\1. Yes, range is a sequence, but it is not a list. Your "output" (which is misleadingly labeled as >>> as if it's a real output of Python, though it's not) is incorrect. range was a list in Python 2, so that's why I said your 'lesson' is outdated (Py2 was killed 20 months ago, and was on life support for more than 5 years).
But there is a bigger problem. What exactly are you trying to teach here? That instead of a for loop, range, list literals, and loop.append method, people should use... range? It makes no sense. Anyone capable of understanding the first code snippet must already know about range. Since it's used in the first snippet also.
\2. Concatenating two dictionaries can be a useful operation to group information... and that's why Python provides an operator for that. a|b is what you need. .update is in most cases not what people need, since it is "concatenation in place" (it modifies a instead of constructing a new value, as other concatenations do). In fact the fact that .update is subtly wrong (and {**a, **b} is even more subtly wrong) has lead to the introduction of a dedicated operator for that operation.
\3. I'm not even sure what are you trying to say here. "creating (in one line) a dictionary where the key depends on x and where the value also depends on x" -- what's x? Do you simply mean a dict comprehension? Why not say so then? Also, I've never seen dicts written with keys and values in parentheses... what's with that? There's really no ambiguity -- x + (2: x**2) + 1 is meaningless anyway.
Also, if keys go from 2 till 6, why not say so directly? for key in range(2, 6).
\4. First, who gave you an idea that reversed function is slower than slicing ::-1? Besides, it's an apples-to-oranges comparison, since these two serve very different purposes. reversed is meant for iterating over the list in the opposite order, without any changing of the list elements' positions, while slicing with ::-1 is meant for making a new list with the same elements as the first one, but in the opposite order.
A bigger problem: not mentioning at all the idiomatic way to do what you wanted. If you want to reverse my_list in place (though it's very rarely needed), my_list.reverse() is what you should write.
\5. You haven't really tried to execute that, right? :-] You have a syntax error there. * on the RHS of assignment has nothing to do with unpacking, it is for re-packing a tuple (or any other iterable) inside something else.
Besides, a, b, c, d = my_tuple is perfectly fine (except the names are horrible, but you know that).
\6. Seriously? 9 lines just to filter 3 and even numbers from a list? Ok, here are some lessons for you. First, my_filter(x) can just be return x==3 or not x%2.
But really, the whole thing is much more easily written (and read) as [x for x in my_list if x==3 or not x%2]. It's funny you explain dict comprehension in 3, but don't use much easier list comprehension in 6.
\7. Generators are very useful, but really, I think noone would call them "return multiple values from a function". My first association was "return one_value, another_value". Look at geeksforgeeks.org/g-fact-41-multip... -- there are 5 things mentioned (2 is what I had in mind) but none of them have anything to do with generators.
\8. Instead of showing how to print ZoPy on the screen, it would be much more useful if you read it and thought about it for a while. ;-) Or at least, inspect.getsource and show how the this module was carefully written to violate every single line in ZoPy. :-D
\9. Those set operations are useful, yes. But they are not the only ones (where's -?), and they aren't even the most useful ones. Beginners usually forget about comparisons, writing horribilities such as s&t == t instead of t <= s.
\10. "It's not the most useful trick, but it's always useful" -- do you read what you write?? Anyway, yes, branching expression is useful, but if your condition is literally True, it is probably the worst example you could find. "Why didn't they just stop writing after i=1?!"
\11. Wat? First, recursion limit has nothing to do with number of recursions, but with depth of any single recursive call. Second, why showing how to change the recursion limit by "changing" it to its default value?
And most importantly, "to avoid having timeouts"? So what, you'd rather have a RecursionError with a 500 frames of traceback, than a correct result at a later time? Ah, kids these days... :-) If you really want to limit the time it takes to execute your function, futures or threads seems like a much more direct (and precise) way to do it that limiting the depth of recursion.
\12. "8 times faster" seems like a very precise number. Putting aside the fact that again you're comparing apples to oranges (print does many things besides calling $file.write), I wouldn't be surprised that looking up the attributes (stdout to sys, then write to that) costs quite a bit of time. Have you measured it, or just read it somewhere? Not everything you read on the internet is correct, you know. ;-)
\13. A death by a thousand cuts. It has nothing to do with middle (as you say later, it can be any position, or even no position at all), underscore is also a variable same as others, and nothing's being "put" anywhere when you assign it a name in Python.
\14. Nice... but again, you could have a more useful example. A billion I'd much rather write as 10**9 than this. But 299_792_458 is much better, for example. ;-)
\15. Yes, but is it any different from item #3? Or did you just write it in order to be able to say you have 15 items? In fact, since it mentions keys and values, maybe a more instructive way would be dict(zip(my_dic.values(), my_dic.keys())). ;-)
You've probably learned something from this. If DEV bans me, many people will be deprived of that opportunity in the future. Just something to think about. Being offended is not necessarily a bad thing. ;-)
First of all, I would like to point out that you are very pretentious and that you are looking for problems that don't exist just to prove to yourself that you are smarter than others, which is actually not the case :)
A piece of advice, come down to earth.
Just because you get stuck on an ex maths test or something like that doesn't mean you have to yell at me. If you need help I can help you if you want :)
1/ It's a mistake on my side, I'm on an IPad so I didn't execute all of these scripts.
2/ The update method is used by several versions of Python while the | operator was only introduced in Python 3.9
3/ I don't want to comment, you're looking for problems where there are none, it's not because there is a shortcut for this case that it's the same everywhere.
4/ You keep looking for problems where there are none. I compare two methods that give the same result, I don't look at how they get there.
5,6,7,8,9,10/ There are no problems, you just want to shout :)
11/ I wasn't clear in my wording, but I will rephrase it as you told me.
12/ I didn't test it by myself, but it's a site to prepare IOI that says it.
13,14,15.1/ Again, there are no problems.
15.2/ It's another method indeed, it's even the one I use, but I wanted to keep it simple in my post which is aimed at beginners.
PS: I will delete our discussion in 24 hours so that I leave you the time to read, but it does not disturb the others (I modified the problem of the range and the unpack)
So, you can just delete what I've written? It's nice to know that. I'll have to be more careful in the future. :-)
I mean, your comment is only constructive on two points, it's more criticism so that you try to prove something to yourself the rest of the time
I understand that it's how you view it. We can discuss it too, but it probably won't be productive -- you're too closed-minded for that.
What was news to me is that you can just delete what I've written. Are you a moderator, or anyone here has absolute power over comments on their posts?
:) maybe I’m a mod
Just like your life
I doubt you know as much about my life as I know about Python. ;-)
Some comments have been hidden by the post's author - find out more