DEV Community

Cover image for 5 Python Tricks in one line. Beginner vs Professional
Vadim Kolobanov for Abstract

Posted on • Edited on • Originally published at dev.to

5 Python Tricks in one line. Beginner vs Professional

Photo by AltumCode on Unsplash

Python is famous for its simplicity and ease of use. This is achieved, among other things, due to the compactness of the code. At the same time, such conciseness may hide complex program logic. In this article, we will look at 5 examples of "syntactic sugar" for processing sequences, when the built-in constructs of the programming language allow you to write less and easier.

Recall that a sequence is called an iterative data structure or simply an array of elements. In Python, sequences such as list (list), string (str), tuple (tuple), dictionary (dict) or set (set) are most often used.

CREATING LISTS IN ONE LINE USING LIST COMPREHENSION

Suppose the task is to create a list whose elements are squares of integers. A beginner in programming will do this way:

squares = []
for x in range(10):
    squares.append(x**2)
Enter fullscreen mode Exit fullscreen mode

As a result:

result
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Enter fullscreen mode Exit fullscreen mode

Note that range is a sequence of numbers from 0 to 10 (not inclusive), and "**" is the exponentiation operator. Now let's do the same as professional Python developers - the List comprehension construct:

squares = [x**2 for x in range(10)]
Enter fullscreen mode Exit fullscreen mode

The result is the same and the code has become more efficient

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Enter fullscreen mode Exit fullscreen mode

List comprehension can be used for complex (nested) loops. For example, let's create pairs of elements from two lists the way a beginner will do it:


first = ['a1', 'a2']; second = ['b1', 'b2']
result = []
for i in first:
    for j in second :
        pair = (i, j)
        result.append(pair)

Enter fullscreen mode Exit fullscreen mode

And now a real Python developer with a good level


result = [(i,j) for i in first for j in second]

Enter fullscreen mode Exit fullscreen mode

That's how with List comprehension you can shorten the process of creating a list from 5 lines to 1. Any programmer will see in your code the hand of a real competent Python developer

CREATING DICTIONARIES USING DICT COMPREHENSION

You need to create a dictionary whose value corresponds to the square of the key. The beginner's method requires creating an empty dictionary and a loop with an element-by-element display of the key and value on the next line:


result = {}
for x in range(10):
   result[x] = x**2

Enter fullscreen mode Exit fullscreen mode

and result...

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Enter fullscreen mode Exit fullscreen mode

Yes, we have completed the task. We are programmers. But a real Python professional will only laugh at you.

He will show you the magic

result = {x: x**2 for x in range(10)}

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
Enter fullscreen mode Exit fullscreen mode

Note that after x there is a colon separating the key and its value. Similar to a List comprehension, Dict comprehension can be used for nested loops. I hope you noticed that the brackets in the List and Dict comprehensions are different.

REMOVING DUPLICATES FROM THE LIST

There are duplicate items in the list, and you only need unique values. What will a beginner do? He will definitely create a new list, and in the loop, he will arrange a check for the compliance of the element with the new list. has O(NĀ²) time complexity.:


fruits = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
new = []
for fruit in fruits:
    if fruit not in new:
        new.append(fruit)

Enter fullscreen mode Exit fullscreen mode

and he will get his result

['banana', 'apple', 'pear', 'orange']
Enter fullscreen mode Exit fullscreen mode

As you can see, apple and orange are no longer repeated. In the 5th line, our newcomer checks whether an item is in the new list: if there is, then adds, otherwise ignores. Our professional, who knows about sets, will surprise us again with a 1-line solution has O(N) time complexity

# List instead of set
fruits = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']

# Actually make `new` a list
new = list(set(fruits))
['banana', 'apple', 'pear', 'orange']
Enter fullscreen mode Exit fullscreen mode

A set is a data structure in which each element is unique. So when you need to exclude duplicate data, use sets

Thanks kbauer, ruudvanderham, jackm767 for useful additions to the article. You can read more information about this item in the comments

IMPORTANT NOTICE!

The set does not contain duplicate elements;

The elements of the set are immutable (they cannot be changed), but the set itself is changeable, and it can be changed;

Since the elements are not indexed, the sets do not support any slicing and indexing operations.

UNPACKING THE ITEMS FROM THE LIST

To pull an element out of the sequence, you want to use the index of the element, right? In some cases, this is correct, but Python allows you to assign each element its own variable.

x, y = (15, 5)

x
15

y
5

fruit, num, date = ['apple', 5,(2021, 11, 7)]

date
(2021, 11, 7)
Enter fullscreen mode Exit fullscreen mode

LISTS SLICING IN PYTHON

In the previous example, we unpacked each item from the list. And what if you need to extract under the list? For example, there is a sequence of 5 elements, you need to take only 3 of them. This is also provided in Python:

x = [1, 2, 3, 4, 5]
result = x[:3]

result
[1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

A more detailed syntax looks like this:

sequence[start:stop:step]
Enter fullscreen mode Exit fullscreen mode

You can omit START if you want to cut from the zero elements, END if you want to cut to the end, and STEP if your default step is 1

For example:


x = [10, 5, 13, 4, 12, 43, 7, 8]
result = x[1:6:2]
result
[5, 4, 43]
Enter fullscreen mode Exit fullscreen mode

Now, knowing the syntactic sugar in Python and how to use it effectively, you can safely be called a competent Python developer

Put on Heart if you liked it and you learned something new!

You can also follow ME to receive notifications about new interesting articles.

FAQ

I am a beginner, how should I learn Python?

Look into the following series:

Learning Python
Step by Step to Junior
Ideas

Would you mentor me?

Of course I am ready to participate in the training. The big difficulty is that English is not my native language and it will be possible to communicate through a translator

Would you like to collaborate on our organization?

If you have interesting ideas, we will be happy to invite your writing from our organization. Write in private messages or in social networks below

If you have interesting projects and you need a backend developer, then you can contact me by mail or in discord for cooperation

Connect to me on

Write me on Face....oh...Meta

My Twitter

Discord: vadimkolobanov#5875

Top comments (26)

Collapse
 
kbauer profile image
kbauer

There are errors in the part about removing duplicates.

  1. fruits = { ... } already is a set, and so the duplicates are already discarded before reaching the loop.

  2. The output of

    new = set(fruits)
    

    is not

    ['banana', 'apple', 'pear', 'orange']
    

    but (order may differ)

    {'banana', 'apple', 'pear', 'orange'}
    

    The output you show would be obtained by

    new = list(set(fruits))
    

Overall, to be correct, the example would have to be changed to

# List instead of set
fruits = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']

# Actually make `new` a list
new = list(set(fruits))
Enter fullscreen mode Exit fullscreen mode

Additionally, for a beginner post it would be very useful to point out that, in addition to be more concise,

new = list(set(fruits))
Enter fullscreen mode Exit fullscreen mode

has O(N) time complexity[1], while

for fruit in fruits:
    if fruit not in new:
        new.append(fruit)
Enter fullscreen mode Exit fullscreen mode

has O(NĀ²) time complexity.

[1] In CPython anyway, which should be using a hash-table based set implementation. Note sure though, whether the loop or the set based solution is faster for small numbers of elements.

Collapse
 
ruudvanderham profile image
ruudvanderham

Under Python 3.6 and higher this one liner will also maintain the order
new = list(dict.fromkeys(fruits))
and it is O(n)!

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

Thank you all so much) You really help all readers to become a little more literate

Collapse
 
kbauer profile image
kbauer

Interesting to know, but hard to read :/ If I'd see this in the code I wouldn't be aware, that preserving the order is an intent here.

Thread Thread
 
ruudvanderham profile image
ruudvanderham

Just adding a comment would help.

Thread Thread
 
vadimkolobanov profile image
Vadim Kolobanov

Guys, I pointed out your help in the article and asked you to pay attention to the comments) So thank you very much! Many opinions, and various practices make this article even better)

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

Thank you so much for the addition. Initially, my goal was just to show the difference in solutions to some problems. I will definitely add your suggestion about algorithmic complexity! And you also gave me the idea to write an article about calculating algorithmic complexity

Collapse
 
qazzquimby profile image
Toren Darby

This comment is very critical, but is not meant to be hostile.

The objective should be clarity, not brevity. Writing shorter code is good only to the extent that it makes the code more readable. "Tricks" in general tend to be obscure, make the writer feel clever, and confuse the reader. Even something like slicing's step parameter should often be explained when it's used.

I recommend people learning python read Google's python style guide to understand the reasoning, not necessarily to follow. They would not encourage a nested comprehension for product. google.github.io/styleguide/pyguid...

"a real Python professional will only laugh at you." The passages like this bother me.
Comprehensions are not dogmatically correct, and someone laughing at a beginner for not using advanced features should not be the face of "real python professionals".

With that out of the way, I think these are useful things to know.
In general I'd advise someone to learn by googling what they're trying to do (eg python remove duplicates from list), look at top answers, and consider what is clearest.

Collapse
 
doroshenkoviktor profile image
Viktor Doroshenko

So agree. I read this article with only thought - those explicit code, which author claims as junior's often is much more readable than suggested oneliners. And claims like "a real competent Python developer code" e.t.c are controversial for me. My experience reading code of a real highly experienced and clever professionals tells me that they usually tend to the simplest and most explicit solution possible. Their code is easy to read and understand. But tricky things usually appear because of ego and lack of self confidence as a professional. Real professional developer visible by his patterns, architecture and code style, not by some tricks.

Collapse
 
xtofl profile image
xtofl

If you replace the nested for loops (plain or in a generator expression) with a call to itertools.product, the reader will immediately see the intent.

result = product(first, second)
Enter fullscreen mode Exit fullscreen mode

Most of the times, this carthesian product is only iterated over and above example will do. If it needs to be stored, you'll need a tuple:

result  = [tuple(*p) for p in
    product(first, second, third)
]
Enter fullscreen mode Exit fullscreen mode

Notice how each added level requires only an added argument to product.

Collapse
 
jackm767 profile image
jackm767

In a post for beginners itā€™s probably worth mentioning that sets are unordered. If you use a set to remove duplicates you can no longer index the data. Converting back to a list may not keep the original order either (? unsure about this).

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

You are absolutely right. It may be worth adding the following

The set does not contain duplicate elements;

The elements of the set are immutable (they cannot be changed), but the set itself is changeable, and it can be changed;

Since the elements are not indexed, the sets do not support any slicing and indexing operations.

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

I am infinitely glad that I can help anyone) Now my friends and I want to create a training course for guys like you. Give you information from experience and practice with homework and supervision. So far, our plan is to do it for free, so that we can learn by ourselves. If it will be interesting, then I left my contacts under the article. Connect with us we want to help you

Collapse
 
Sloan, the sloth mascot
Comment deleted
 
vadimkolobanov profile image
Vadim Kolobanov

Š—Š“рŠ°Š²ŃŃ‚Š²ŃƒŠ¹Ń‚Šµ) Š“Š»Ń Š¼Š¾ŠµŠ¹ ŠøŠ“ŠµŠø Š²Ń‹ ŠæрŠ¾ŃŃ‚Š¾ Š·Š¾Š»Š¾Ń‚Š¾) ŠŸŃ€Š°Š²Š“Š° я ŠæŠ¾ŠŗŠ° Š½Šµ сŠ¾Š²ŃŠµŠ¼ сфŠ¾Ń€Š¼ŠøрŠ¾Š²Š°Š» ŠŗŠ¾Š½ŠµŃ‡Š½ŃƒŃŽ Š·Š°Š“уŠ¼Šŗу Š½Š¾ я с уŠ“Š¾Š²Š¾Š»ŃŒŃŃ‚Š²ŠøŠµŠ¼ с Š²Š°Š¼Šø ŠæŠ¾ŃŠ¾Ń‚Ń€ŃƒŠ“Š½ŠøчŠ°ŃŽ. Š¢Š°Šŗ ŠŗŠ°Šŗ Š°Š½Š»ŠøŠ¹ŃŠŗŠøŠ¹ яŠ·Ń‹Šŗ у Š¼ŠµŠ½Ń тŠ¾Š»ŃŒŠŗŠ¾ Š“Š»Ń чтŠµŠ½Šøя Š“Š¾ŠŗуŠ¼ŠµŠ½Ń‚Š°Ń†ŠøŠø ŠæŠ¾Š“хŠ¾Š“Šøт)

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
vadimkolobanov profile image
Vadim Kolobanov

ŠšŠ°Šŗ Š²Š°Š¼ уŠ“Š¾Š±Š½Š¾) Š±Ń‹Ń‚ŃŒ Š¼Š¾Š¶ŠµŃ‚ Š²Ń‹ Š¼Š½Šµ тŠ¾Š¶Šµ ŠæŠ¾Š¼Š¾Š¶ŠµŃ‚Šµ сфŠ¾Ń€Š¼ŠøрŠ¾Š²Š°Ń‚ŃŒ ŠŗŠ¾Š½ŠµŃ‡Š½ŃƒŃŽ ŠøŠ“ŠµŃŽ)

 
vadimkolobanov profile image
Vadim Kolobanov

Š½Š°ŠæŠøшŠøтŠµ Š¼Š½Šµ Š² Š“ŠøсŠŗŠ¾Ń€Š“ ŠµŃŠ»Šø Š½Šµ сŠ»Š¾Š¶Š½Š¾ vadimkolobanov#5875

Collapse
 
foxy4096 profile image
Foxy4096

Love this post, it will surely help me.

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

I am very happy about it! If my work helped at least 1 person, then it was not in vain and my goal was achieved)

Collapse
 
sentayehu profile image
Sent

Removing duplicate in case insensitive list.
eg fruits=['banana', 'Banana', 'Mango', 'mango', 'orange', 'OranGe']
set(map(str.lower, fruits))
will give you: {'orange', 'banana', 'mango'}

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

It's very simple) You are trying to change the values of the set. You forget that the values in it are immutable, so the lower case will not work. In this situation, it is necessary to bring to one register earlier

Collapse
 
sentayehu profile image
Sent

Right, but set is applied right after we lower the case for all items.
demo: online-python.com/isJQnjgrZf

Collapse
 
aatmaj profile image
Aatmaj

Nice. But I personally prefer the beginner versions.

Collapse
 
hikionori profile image
Danila Verbinsky

It seems to me that all novice developers know about generators, since they are used in all tutorials, and you will not knowingly know how to use them.

Collapse
 
vadimkolobanov profile image
Vadim Kolobanov

if you look at the projects on github, you can see that there are almost never such solutions or generators in the principle. I mean newbie accounts with 1 repository. They know but they don't use it