Edgica logo

Python is a programming language with clear syntax, and many convenient things, due to their simplicity, often do not linger in the memory. In this case, the most concise decisions are usually the fastest. Here we provide some simple python tricks that will be interesting for beginners and experienced.

1. Combining lists without a loop

How would you solve the problem of combining lists of different lengths without going around loop elements? Here’s how to do it with the standard sum function:

L = [[1,2,3], [4,5], [6], [7,8,9]]
print(sum(L, []))

The results:


Even if a shorter, but more efficient way is to use the itertools module:

import intertools
L = [[1,2,3], [4,5], [6],)

Note that when working with sequences, many useful solutions are in the modules of the standard collections library (container data structures) and intertools (operations on sequences). A careful reading of the documentation of the modules will free you from many hours of inventing your own “bicycles”.

2. Exchanging of values ​​using tuples

One of the most popular Pethin tricks is exchanging values ​​without creating a temporary variable. The method is applicable to any number of variables.

a, b = 1, 2
print(a, b)
a, b = b, a
print(a, b)

The results:

1 2
2 1

In the right part of the sequence assignment instruction, you can specify iterative objects. The main thing is that the number of elements on the left is equal to the number of elements on the right. This assignment also applies to complex nested constructions:

for ((a, b), c) in [((1, 2), 3),((4, 5), 6)]:
print(a, b, c)

The results:

1 2 3
4 5 6

3. Unpacking sequences with an unknown number of elements

For the case indicated in the subtitle, Python 3 has an asterisk operator — an extended sequence unpacking operation. A variable with an asterisk is assigned a part of the list containing all unassigned elements corresponding to this position:

seq = [1, 2, 3, 4]
*a, b, c = seq
print(a, b, c)
a, *b, c = seq
print(a, b, c)
a, b, *c = seq 
print(a, b, c)
a, b, c, *d = seq
print(a, b, c, d)
a, b, c, d, *e = seq
print(a, b, c, d, e)

The results:

[1, 2] 3 4
1 [2, 3] 4
1 2 [3, 4]
1 2 3 [4]
1 2 3 4 []

You can carry out similar operations using slices, but such code looks more natural. The extended unpacking operation is also used in cycles when the length of the nested sequences varies:

for (a, *b, c) in [(1, 2, 3), (4, 5, 6, 7)]:
  print (a, b, c)

The results:

1 [2] 3
4 [5, 6] 7

4. String concatenation

In program code, one often encounters string concatenation with the addition sign. It is more convenient to create a string from a list of several substrings using the string join method.
An example is more complicated with the join method – converting a list of numbers into a string:

numbers = [1, 2, 3, 4, 5]
print (','.join(map(str, numbers)))

The results:


5. Anagram check

Checking whether the strings are anagrams (for example, as a result of a random permutation of letters) will help the Counter class of the collections module:

from collections import Counter

str1 = 'edgica'
str2 = 'degica'

print(Counter(str1) == Counter(str2))

The results:


6. Delete duplicates in a list

Among the regularly used tricks on Python is converting a list to a set and back to a list to remove duplicate list items:

items = [2, 2, 3, 3, 1]


The results:

[1, 2, 3]

But sets are disordered sequences. Often the task is to preserve the sequence of elements. It is convenient to use the OrderedDict data type from the collections module:

items = [2, 2, 3, 3, 1]
from collections import OrderedDict
print (list(OrderedDict.fromkeys(items).keys()))

The results:

[2, 3, 1]

7. Default value output for missing dictionary key

A call to a nonexistent dictionary key throws an exception. This can be avoided by calling the get method. In this case, the method returns None (default) or the specified value of the argument.

d = {'a':1, 'b':2}

The results:


When creating your own data type based on dictionaries, pay attention to the __missing__ method to return an argument in the absence of a key:

class MyDict(dict):
  def __missing__ (self, key):
    return key

D = MyDict(a=1,b=2)

The results:

{'a': 1, 'b': 2}

8. Output using print

It is often pointed out that the main difference between Python 2 and Python 3 is the brackets after the print statement. This also means that the print statement has become a function, which means that the brackets may include some additional arguments.

Print has the following arguments:

  • sep string (by default, one space) inserted between objects during output;
  • end string (default \ n), added to the end of the displayed text;
  • file (by default sys.stdout) – any object that supports the write (string) file method, i.e. a standard stream, file, etc.

For example, if we do not need to combine substrings, we just print the total string:

for part in ["edg", "ica", ".com", "\n"]:
  print(part, end='')

The results:


The same approach can be used to read files:

for line in open('example.py'):
  print(line, end='')

Assigning an empty argument to the end argument causes the lines of the file to not be interleaved with empty lines. Otherwise, when reading the lines of a file and using end by default, the line ending character \ n would be repeated twice.

9. Numbered lists

The task of numbering sequence elements is so common that Python has a corresponding built-in enumerate function:

for i, item in enumerate(['a','b','c']):
  print(i, item)

The results:

0 a
1 b
2 c

For those who are already familiar with enumerate, it may turn out to be news that the function has a second argument specifying the initial number:

for i, item in enumerate(['a', 'b', 'c'], 1):
  print(i, item)

The results:

1 a
2 b
3 c

10. Finding the most frequently repeated list items

You can find the most frequently repeated element using the built-in max function. The max function is able to search for the greatest value not only for the iterable object itself, but also based on the results of applying the function to it. Having converted the list into a set (see trick 6) and using the count method to find the number of occurrences of an element in the list, we get:

 a = [1, 2, 3, 1, 2, 3, 2, 2, 4, 5, 1]
print(max(set(a), key=a.count))

The results:


If you need to find some of the most frequently repeated values, use the counter from the collections library:

from collections import Counter

a = [1, 2, 3, 1, 2, 3, 2, 2, 4, 5, 1]
cnt = Counter(a)

The results:

[(2, 4), (1, 3), (3, 2)]

The most_common method displays a list of tuples of the view (element, number of repetitions). The argument corresponds to the desired number of tuples. By default, a list of tuples is displayed for all elements of the transferred list.

Copyright 2024 Edgica LLC, All rights reserved
Subscribe to our Newsletter!

Subscribe to our Newsletter!

Join our mailing list to receive the latest news and updates from Edgica. We keep your contact information confidential, you always can unsubscribe.

Thank you! Please, check your Inbox to confirm the subscription!

Pin It on Pinterest

Share This