13 Trendiest Python Techniques for Effective Coding

  1. Flatten the lists

Iterators are technically objects that implement Python’s iterator protocol, which is made up of the methods __next__() and __iter__ ().

When employing the for and in keywords, the __iter__() method returns the iterator object itself.
The next value is returned by the __next__ method. Once all the objects have been monitored, it also returns a StopIteration error.
For loops are the main application of iterators. The Python Handbook contains a detailed description of them.

The subsequent statements will import itertools into your Python code.

import itertools
a = [[a, b], [c, d], [e, f]]
b = list(itertools.chain.from_iterable(a))
print(b)
Output:
[a, b, c, d, e, f]

2. Reverse a list

a=[“d”,”c",”b",”a"]
print(a[::-1])
Output:
d
c
b
a
or# create a list of prime numbers
prime_numbers = ["a", "b", "c", "d"]
# reverse the order of list elements
prime_numbers.reverse()
#for stringsa=”python”
print(“Reverse is”, a[::-1])
Output:
Reverse is nohtyp

3. Combining different lists

a=[‘a’,’b’,’c’,’d’]
b=[‘e’,’f’,’g’,’h’]
for x, y in zip(a, b):
print(x,y)
Output:
a e
b f
c g
d h

4. Negative Index Listing

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a[-3:-1]
Output:
[8, 9]
#Works as a substring for array

5. Examining the most common items on the list

If you want to find the most used value in array

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

6. Splitting the string by separator

a=”Bora Erbasoglu is a good writer who writes development articles”
b=a.split()
print(b)
Output:
[‘Bora’, ‘Erbasoglu’, ‘a’, ‘good’, ‘writer’, ‘who’, ‘writes’, ‘development’, ‘articles’]

7. Multiple string values printed out

print(“good”*3+’ ‘+”bad”*2)Output:
goodgoodgood badbad

8. Creating a single string from an array by joining them pre-defined string

a = [“We”, “are”, “not”, “alone”]
print(“ “.join(a))
Output:
We are not alone

9. Looking for anagrams between two words

from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
print(is_anagram(‘good’, ‘odge))
print(is_anagram(‘bad’, ‘bed’))
Output:
True
False

10. Implementing the map function (best use example)

You might encounter an input like this in competitive coding:
1234567890

Follow these steps to retrieve the input as a list of numbers:
input, list(map (int) ().
split()))

Note: Regardless of the kind of input, always use the input() function and convert it with the map function.

map(int, input) list (“enter numbers:”).split()))
Put numbers here:
1 2 3 4 5 6 7
[1, 2, 3, 4, 5, 6, 7]

One of Python’s most helpful built-in features and functions is the map function.

11. How to merge two different list

Duplicates can be eliminated from a list using the Collections module. Duplicate module removal requires the HashMap in Java, while Python makes it much simpler.

print(list(set([1,2,3,4,3,4,5,6,7,8,9])))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

When combining multiple lists, you must utilize the extend() and append() functions in the lists.

a = [1, 2, 3, 4] # list 1
b = [5, 6, 7, 8, 9] # list 2

Keep in mind that >>> a.extend(b) only shows one list.

a [1, 2, 3, 4]

Keep in mind that >>> a.append(b) will show the list of lists.

a [1, 2, 3, 4 [5, 6, 7, 8, 9]]

12. Writing code within functions

It is always preferable to write your Python code inside of functions.

function main(): print(x) main for I in range(2**3) ()

The code snippet above is superior to the one below:

print for x in the range (2**3) (x)

When saving local variables, the CPython implementation is faster.

It is important!

With the aid of these helpful Python tips, you may write more effective and effective code. Here is a supplementary suggestion that you ought to be aware of and use.

13. Concat Strings

empty_str = ""
best_string = ["Welcome", "to","the","jungle"]; str1 = “”
print(empty_str.join(best_string))

Instead of, use the above code:

best_string = ""
best_list = ["Welcome", "to","the","jungle"]
for x in best_list:
best_string += x
print(best_string)

Better code depends on speed and efficiency above everything else. Your Python programming abilities will be considerably enhanced if you use the advice provided in this article. Try them out in your upcoming Python projects or competitive coding competition to see the impact they have.

--

--