DEV Community

Lamri Abdellah Ramdane
Lamri Abdellah Ramdane

Posted on

11 Essential Python Tips Every Developer Should Know

Becoming a professional Python developer isn’t just about writing code that works—it’s about writing clean, efficient, and Pythonic code. Here are 11 essential Python tips that will help you level up your skills.


1. Use enumerate to Get Index and Value

When iterating through a list, many developers still rely on range(len(data)). It works, but it’s not very Pythonic.

Example (not recommended):

data = [1, 2, -3, -4]
for i in range(len(data)):
if data[i] < 0:
data[i] = 0

data becomes [1, 2, 0, 0]

A better way is to use enumerate, which gives you both index and value directly:

data = [1, 2, -3, -4]
for idx, num in enumerate(data):
if num < 0:
data[idx] = 0

data becomes [1, 2, 0, 0]


2. Understand Assignment: Aliases vs Copies

In Python, assigning a variable doesn’t copy the object—it creates an alias.

a = [1, 2, 3, 4, 5]
b = a
b[4] = 7

print(a) # [1, 2, 3, 4, 7]

Both a and b point to the same object. If you want a copy:

b = a.copy() # or b = a[:]
b[4] = 7

print(a) # [1, 2, 3, 4, 5]
print(b) # [1, 2, 3, 4, 7]


3. Prefer F-strings for String Formatting

Since Python 3.6, f-strings are the most readable and efficient way:

first_name = "Lily"
age = 18
print(f"Hi, I'm {first_name} and I'm {age} years old!")


4. Use Generators to Save Memory

List comprehensions load everything into memory, while generators create values on demand.

import sys

my_list = [i for i in range(10000)]
print(sys.getsizeof(my_list))

my_gen = (i for i in range(10000))
print(sys.getsizeof(my_gen))

Generators are memory-efficient, but they can only be consumed once.


5. Safely Access Dictionaries with .get()

Direct access risks KeyError:

my_dict = {'item': 'football', 'price': 10.00}
count = my_dict.get('count', 0)
print(count) # 0


6. Use zip to Iterate in Parallel

names = ['Amy', 'Ben', 'Clara']
scores = [85, 80, 95]

for name, score in zip(names, scores):
print(f"{name}: {score}")


7. List Comprehensions for Compact Loops

Filter and transform in one line:

numbers = [1, 2, 3, 4, 5, 6, 7, 8]
evens = [num for num in numbers if num % 2 == 0]

processed = [num * 2 if num % 2 == 0 else num for num in numbers]


8. Simplify Counting with defaultdict

from collections import defaultdict

words = ['apple', 'banana', 'apple', 'orange']
word_count = defaultdict(int)
for word in words:
word_count[word] += 1


9. Remove Duplicates from Lists

Use set for uniqueness:

a = [1, 1, 2, 3, 4, 5, 5]
unique = list(set(a))

Or preserve order:

ordered_unique = list(dict.fromkeys(a))


10. Join Strings Efficiently

list_of_strings = ["Hello", "my", "friend"]
my_string = " ".join(list_of_strings)
print(my_string)


11. Reverse a Dictionary in One Line

dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {v: k for k, v in dict1.items()}

print(dict2) # {1: 'a', 2: 'b', 3: 'c'}


Final Thoughts: Tools Matter Too

These Python tips can make your code cleaner and more Pythonic. But productivity isn’t just about coding tricks—it’s also about having the right tools.

Managing multiple Python versions across different projects can be painful. That’s where ServBay comes in. It lets you install Python with one click and switch between versions easily. Perfect for working with older dependencies or testing new features.

A good tool can save you hours and let you focus on writing better code. 🚀

Top comments (0)