What I love most in Python is its readability and ease of use, which makes Python a good language to start in programming. To make Python learning easier, I will list some useful tips and tricks. Please note that I use Python 3.9 and you must have basic Python knowledge to use it properly.
1. Sorting a list of lists or tuples or dicts
We can use the key
parameter in the sort
function to provide sorting criteria. We can do this to sort nested lists or tuples based on their last element. If we don't specify a key, it will sort based on the first attribute.
>>> a = [[2, 3, 4], [1, 1, 5], [7, 0, -1]]
>>> sorted(a, key=lambda b: b[-1])
[[7, 0, -1], [2, 3, 4], [1, 1, 5]]
If the list contains a dict, just change the key to key=lambda b: b['some_key']
. If we want to reverse the sorting, add reverse=True
.
>>> sorted
(a, key=lambda b: b[-1], reverse=True)
[[1, 1, 5], [2, 3, 4], [7, 0, -1]
For more complex sorting, provide a callback as the key
2. Multiple assignment
We can assign multiple values in one line. In the example below, we assign 4 values to 3 variables. But as b
has an asterisk in it, it will get values assigned to it as a list.
>>> a, *b, c = 1, 2, 3, 4
>>> a
1
>>> b
[2, 3]
>>> c
4
When the number of values equals the number of variables, it will be stored as a list with one element.
>>> *b = 1
>>> b
[1]
3. Swapping variables
Swapping two variables can be easily done with this trick.
>>> a, b = 1, 2
>>> print(a, b)
1, 2
>>> a, b = b, a
>>> print(a, b)
2, 1
4. Combining strings
We can use str.join()
to join list of strings, by providing a string to be used as concatenator. See that the string we provide (' and ') is placed between each element, like between Anna and Bob.
>>> persons = ['Zakki', 'Anna', 'Bob', 'Charlie']
>>> ' and '.join(persons)
'Zakki and Anna and Bob and Charlie'
5. Put variables to string
We could use f-string to put variables to string.
>>> subject = 'Zakki'
>>> object = 'window'
>>> f'{subject} is looking at the {object}'
'Zakki is looking at the window'
6. Merge dictionaries
>>> a = {'a': 1}
>>> b = {'b': 2}
>>> d = {**a, **b, **{'c': 3}}
>>> d
{'a': 1, 'b': 2, 'c': 3}
You can also use this (Python 3.9+ only)
>>> d = a | b | {'c': 3}
>>> d
{'a': 1, 'b': 2, 'c': 3}
7. Map elements
Using this trick we can map an element in one list or tuple to another list or tuple
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> list(zip(a, b))
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> list(zip(b, a))
[('a', 1), ('b', 2), ('c', 3)]
8. Ternary operator
Use simple this one line ternary operator with syntax value_when_true if condition else value_when_false
>>> a = 'Big'
>>> b = 'Small' if a == 'Big' else 'Big'
>>> b
>>> 'Small'
Instead of
if a == 'Big':
b = 'Small'
else:
b = 'Big'
9. Removing duplicates
By converting to set
, we get the unique values.
>>> a = [1, 1, 2, 2, 3]
>>> a
[1, 1, 2, 2, 3]
>>> list(set(a))
[1, 2, 3]
10. Find elements showing in both lists
We can do that by converting to set
, then use set.intersection()
>>> a = [1, 2, 3]
>>> b = [1, 2]
>>> set_a = set(a)
>>> set_b = set(b)
>>> set_a.intersection(set_b)
{1, 2}
I hope this article helps you in many ways.
Share on Twitter Share on Facebook
Comments
There are currently no comments
New Comment