Bits of Python Information

List comprehension instead of map and filter

List comprehensions provide a concise way to create lists. List comprehensions are clearer than the map and filter built-in functions because they don't require extra lambda expressions.

# syntax: [expression for item in iterable if conditional]

# instead of using
doubled_numbers = list(map(lambda x: x * 2, range(10)))

# use
doubled_numbers = [x * 2 for x in range(10)]

# both gives the output
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Do not write Python as C

Developers that have C/C++ background can write python code as C in first times. You should avoid this for writing more readable, faster, and pythonic code.

# instead of writing a loop like
for index in range(len(a_list)):
    print(a_list[index])

# write like 
for item in a_list:
    print(item)

# if you also need the index of item you can use enumerate
for index, item in enumerate(a_list):
    print(index, item)

Swapping values

In Python, you don't need a temporary variable to swap values. You can do it in one line.

x_val, y_val = y_val, x_val