2019-06-07 21:35:15 (edited by majno 2019-06-07 21:46:12)

Sorry.  mistake in post 24. instead of != it should be ==.
Post edited.
Not all items are checked when an item is removed because if item in index 1 is removed the item number 2 in index will replace item number 1.
so loop index will continue with next item number 2 skipping item number 2 which now is item number 1.

Thumbs up

2019-06-07 22:05:29

@26, I'm not sure what your trying to convey here. An LC with a for loop is exactly the same as a normal for loop but more compact. You will get exactly the same results no matter how you do it.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2019-06-08 19:08:10

To all the people saying list comprehensions are bad because they are hard to understand, I beg to differ.

The only reason you might think that is because you are used to the long form version and once you get used to  list comprehensions they are much much more readable and practical, because instead of drawing out the same info it is super compact. Also because it describes what you want instead of how to get what you want.

LCs are even more useful for more complex  things like if you want to apply a function to transform the elements.

# generate first 10 squares
squares10 = [num * num for num in range(1,11)]

Super simple and  long form version is:
>>> squares10 = []
>>> for num in range(1,11):
...  squares10.append(num*num)

Way longer, easier to make a mistake, and less descriptive.

nested LCs are also fantastic and pay off even more. To give  very simple example, imagine you want to generate a big list of possible names for bots or anonymous users, etc.

>>> adjectives = ["Happy", "Angry", "Hungry"]
>>> animals = ["Lion", "Zebra", "Hippo"]
>>> names = [adj + animal for adj in adjectives for animal in animals]
>>> names
['HappyLion', 'HappyZebra', 'HappyHippo', 'AngryLion', 'AngryZebra', 'AngryHippo', 'HungryLion', 'HungryZebra', 'HungryHippo']

P.S. I know there are other ways to enumerate possible combinations. This is mostly just an example, but one ting to keep in mind is that LCs are more versatile.

Thumbs up

2019-06-08 21:22:06

@28, agreed, but LCs are only useful in particular scenarios. They're also limited.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2019-06-08 22:36:33

@23
no
I got the following mixed
x = [item for item in x if item!= 3]
with
x = (item for item in x if item!= 3)
Put that in the console and you get a generator.

Twitter: stevo399
Website: stevend.net
Have a nice day!

2019-06-08 23:37:32

@30, Ah. I see now.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.

Thumbs up

2019-06-09 20:37:00

@24 and 26, this is not true. Your for loop would only skip one index if you defined it like "for i in range(0, 10)". The syntax with "for item in list" never skips an index.

We are pleased, that you made it through the final challenge, where we pretended we were going to murder you. We are throwing a party in honor of your tremendous success. Place the device on the ground, then lay on your stomach with your arms at your sides. A party associate will arrive shortly to collect you for your party. Assume the party submission position or you will miss the party.

Thumbs up

2019-06-09 20:43:13

@28, I agree.

if you wish,  you could give a look at my github profile.
If you want to get in touch with me you can follow me on Twitter
have a nice day.
Paul