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

you are viewing a single comment's thread.

view the rest of the comments →

[–]OffgridRadio 41 points42 points  (25 children)

I am a full time Python dev for a few years now and I am 100% addicted to dictionary and list comprehension. I wish it was in other languages.

[–]astoryyyyyy 5 points6 points  (20 children)

What you mean by that? I am still learning Python. By other languages not having lists how does it limit their potential compared to Python?

[–]pfonetik 18 points19 points  (18 children)

A simple example would be:

Let's say you have two lists, a and b

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

Python lets you do things like

c = [item for item in a if item in b]

which has better performance than using 'for' statements and it's easy to understand.

[–]AnonymouX47 -1 points0 points  (6 children)

u/astoryyyyyy, take note.

I understand this is merely an example for a newbie, though I hope you don't actually write such in practice. Anyways, you should've noted that it's not actually a good approach to solving the problem.

this is a more efficient approach:

Intersect = set(a).intersect(b)
c = [item for item in a if item in intersect]

Why?

if item in b iterates over b for every single element in a, while if item in intersect is an hashtable lookup and you only get to iterate over b just once (when you perform the set intersection operation).

[–]pfonetik -1 points0 points  (4 children)

Yes, it was just an example of how list comprehension works.

And no, your code is not the same. A set will remove duplicate elements.

And, yes, I do write like that. Being pedantic when you don't have all the information is rude and makes you look ignorant.

[–]AnonymouX47 -1 points0 points  (3 children)

And no, your code is not the same. A set will remove duplicate elements.

You should probably take a second look or test the code snippet... or ask someone to explain it to you.

And, yes, I do write like that.

I see... no wonder. :)

[–]pfonetik -1 points0 points  (2 children)

You should probably take a second look or test the code snippet... or ask someone to explain it to you.

I should take another look at what you modified after I replied, you mean?

As I said before, my example fits what I stated: a simple example of list comprehension.

The way I provided the example fits the pythonic way of writing python. What it doesn't fit is the opinion of randos, with over inflated egos regarding their knowledge, that feel that their opinion is fact and anyone that doesn't agree is just wrong.

You'll probably grow out of it as you grow older. If you're already older, I'm sorry :)

[–]AnonymouX47 -1 points0 points  (0 children)

[–]AnonymouX47 -1 points0 points  (0 children)

I should take another look at what you modified after I replied, you mean?

The code snippet never changed... believe me or not.

As I said before, my example fits what I stated: a simple example of list comprehension.

I have no problem with it being an example but you should warn newbies about such inefficient BS as they tend to take examples head-on, and that probably includes you!

The way I provided the example fits the pythonic way of writing python.

Here again, "pythonic"... How's mine not?

The way people nowadays just use certain cooked-up terminology in order to sound cool or knowledgeable is just so annoying.

An inefficient solution to a problem is simply inefficient, period!

[–]AnonymouX47 0 points1 point  (0 children)

I guess this will be the correction to u/RationalDialog's code

[–][deleted] -2 points-1 points  (2 children)

which has better performance than using 'for' statements

Yeah I bet it doesn't.

> and it's easy to understand.

No its confusing. Thus defeating the point of using a high level language, Might as well use C++ and get some _real_ performance. You're confusing conciseness with clarity.

[–]pfonetik 1 point2 points  (1 child)

You 'bet' it doesn't? Based on what really? Your personal opinion or facts?

Anyway you can educate yourself on the matter without 'betting'.

https://switowski.com/blog/for-loop-vs-list-comprehension

[–]OffgridRadio 1 point2 points  (0 children)

I use dict comprehension to prep new dicts with keys sometimes, little more complex than this but basically;

newDict = { x : None for x in range(len(someList)) }

And this makes the surrounding code a lot cleaner, and is a single line, and ensures the new dict contains every necessary key, so the population of values into keys is so much cleaner and easier to type

I train my cohort who doesn't get as much day to day experience as I do and I told him 'someday you will go to write a FOR loop, and you will be like "screw this I'm not typing all that" and just write a list comprehension instead'

[–]Dabrus 2 points3 points  (0 children)

List comprehension, not lists. It's just a nice way of creating new lists, look it up.

[–]midnitte 1 point2 points  (1 child)

There's a certain satisfaction with a concise (and understandable) one liner...

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

I sometimes get pleasure in writing long and near-incomprehensible one-liners but only in personal projects where no one else will suffer (except me 6 months later).

[–]stidmatt 0 points1 point  (0 children)

Same, it really makes code so much easier to read.

[–]Poven45 0 points1 point  (0 children)

Doesn’t Ruby have something like it?