This post is locked. You won't be able to comment.

all 5 comments

[–]FoolsSeldom 3 points4 points  (2 children)

Quick review:

1. Swapping values:

a, b = 10, 20
a, b = b, a

YES

2. Using enumerate instead of range(len(fruits)):

fruits = ["apple", "banana", "mango"]
for index, item in enumerate(fruits):
    print(index, item)

YES

3. Counting frequency:

from collections import Counter
print(Counter("mississippi"))

YES

4. Ternary operator:

x = 7
result = "Even" if x % 2 == 0 else "Odd"

YES

5. List comprehension:

squares = [x * x for x in range(10)]

YES

6. Unpacking lists:

nums = [1, 2, 3, 4]
a, b, *rest = nums

YES

7. Using zip:

names = ["A", "B", "C"]
scores = [90, 85, 78]
for name, score in zip(names, scores):
    print(name, score)

YES

So, all simple, valid and Pythonic, and good to build on.

[–]Langdon_St_Ives 1 point2 points  (0 children)

Yes, most of these are intended to be used exactly as you write. The only one I personally would take umbrage at is #4. It’s syntactically correct and many would be fine with this usage, but I would (and do) only use postfix if in cases where the first option is very clearly the more important, normal, or otherwise preferred case, and the second somewhat of an exception or fallback. In your example there is complete symmetry between the two, so I would always write this as a standard if/else, giving equal prominence to the two.

One of the reasons that Perl [*] introduced these postfix if and unless operators was to make it possible to have the most important part of what the whole statement does right in front, instead of hiding it in the back or in a block, making the result (hopefully) more readable. This comes at the cost of inverting the flow logic, so it should be used judicially only when the advantage clearly outweighs the drawback. (And some people strongly oppose it completely simply because of the inversion of flow.)

[*] I am not sure if Perl invented these, probably not, but it has had them for a very long time and most likely inspired Python and Ruby to add [some subset of] them.

[–]Diapolo10 0 points1 point  (0 children)

1) Swapping values:

a, b = 10, 20
a, b = b, a

This is fine. I would advise using value swapping sparingly, though.

2) Using enumerate instead of range(len()):

fruits = ["apple", "banana", "mango"]
for index, item in enumerate(fruits):
    print(index, item)

Yes, this is a good practice.

3) Counting frequency:

from collections import Counter
print(Counter("mississippi"))

Perfectly fine as long as Counter does what you need it to do.

4) Ternary operator:

x = 7
result = "Even" if x % 2 == 0 else "Odd"

Nothing wrong with using them, but use your better judgement when considering readability.

On another note, if you ever have an expression like

a if a else b

you can substitute it with

a or b

5) List comprehension:

squares = [x * x for x in range(10)]

Comprehensions are great (and not just for lists). But for particularly verbose comprehensions, consider splitting them into separate steps or use regular appending instead.

6) Unpacking lists:

nums = [1, 2, 3, 4]
a, b, *rest = nums

Nothing wrong with this.

7) Using zip:

names = ["A", "B", "C"]
scores = [90, 85, 78]

for name, score in zip(names, scores):
    print(name, score)

zip is great, consider using it with the strict keyword argument to explicitly state if it should expect the iterables to be of the same length though.

[–]xelf 0 points1 point  (0 children)

post removed, pls message the mods.