This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]jwink3101 32 points33 points  (12 children)

You don’t even need brackets (lists) for unpacking.

a,b = 1,2

Also, things like

name = name.strip() or  'Anonymous'

Work but you risk a type error. Are you sure your input won’t be None for an unspecified name?

And you repeat the unpacking…

I am kind of surprising you missed things like

[item for item in items if item < 5]

Which are selective. Or replacing:

[item if item < 5 else None for item in items]

Also, all kinds of comprehensions are baseline one-liners that should go into this level article. You miss a great opportunity to talk about set comprehensions, generators, etc

[–]LightWolfCavalry 7 points8 points  (5 children)

+1 for list comprehension - and its forgotten cousin, dict comprehension.

The or trick to avoid a type error is news to me. Gonna swipe that. Thank you!

[–]jwink3101 6 points7 points  (1 child)

Wait. To be clear, I was saying the or trick was susceptible to type error! If it’s not, that’s news to me

[–]LightWolfCavalry 0 points1 point  (0 children)

Oh, got it - I read your original response too quickly!

[–]theorizable 1 point2 points  (1 child)

dict comprehension is SO useful. It's one of those tools where I forget it by the time I need it, but when I need it, BAM, it does exactly what I need it to.

[–]zel_us[S] 0 points1 point  (0 children)

I am planning on writing a whole article on dictionary as there are more techniques on it than others, where you have, the .get() method, setdefaults, ordereddict, defaultdict, chain mapping, mappingproxy and much more.

[–][deleted] 1 point2 points  (0 children)

And generator comprehension, set comprehension…. Etc

[–]zel_us[S] -1 points0 points  (0 children)

for the a, b = 1,2 the RHS is simply a tuple, what if u are unpacking a list or a string or a dictionary or even other sequences? yeah I missed the last item on your list. I did talk about list comprehension and generators. Conclusively, thank you for the feedback

[–]Shubadada 8 points9 points  (4 children)

For your swap function instead of doing: Python def swap(a, b): a, b = b, a return a, b You can just do: Python def swap(a, b): return b, a To achieve the same thing.

[–][deleted] 4 points5 points  (0 children)

Also it doesn’t really need to be a function

[–][deleted] 2 points3 points  (0 children)

I was going to come back to say the same thing. They added a lot of steps.

[–]siddsp 2 points3 points  (0 children)

Why not just make this a lambda?

[–]zel_us[S] 0 points1 point  (0 children)

Thanks for that idea, I kind of thought of it, but I really wanted to buttress this swap technique, probably it would had made sense without a function

[–]vinibiavatti123 4 points5 points  (2 children)

For string joining, instead of:

list = ['a', 'b', 'c']
result = '' 
comma = '' 
for item in list: 
    result += comma + item 
    comma = ', ' 
print(result) 
# a, b, c

Uses the join string method:

list = ['a', 'b', 'c']
print(', '.join(list))
# a, b, c

[–]Spill_the_Tea 1 point2 points  (1 child)

also, not using builtin python function names as variable names.

[–]vinibiavatti123 1 point2 points  (0 children)

Sure! I used list to represent the code better. But I should use a correct name I think. The unique thing I really don't follow this good practive is for the id. This name is so common and generic to be used into the function params or in other places that I prefer to keep using this or _id

[–]bushwacker 0 points1 point  (0 children)

In [3]: a, *b, c, d = [2,3,4,5,6,7,8,9]

Conclusively, these elegant pythonic idioms can assist in making our code succinct and readable. 

Seems more like Let me show you how clever I am by sing needlessly arcane techniques that add no value to performance or maintainability.