last day (15 days later) » 

19:33
-1
A: How to replace all list of list items with the index value of that list

Edward PetersHere is a solution with the map function: res = list(map(lambda i : [i] * len(example[i]), range(len(example)) )) print(res) It's more verbose than the comprehension method, but coming from other languages I prefer it - I find the map to be more explicit and easier to follow,...

I disagree that this is easier to follow. It's a lot more verbose, for starters.
But it doesn't require you to know the ins and outs of python for comprehensions. For me, I find that the comprehension model looks nice, but it's harder to disentangle to what exactly is done with each position, while map is a concept any programmer in a modern language should grok. Verbosity can mean clarity.
List comprehensions are not unique to Python, though.
I'm not sure of other languages that use python-esque syntax, but the map model exists in every halfway decent language I'm familiar with. I also just find comprehensions get messy as you start to add to them (like, nested comprehensions, if expressions, whatever.) As someone who switches between languages often, I'd rather use the very common and standard language of function calls, rather than try to piece together exactly what will bind to what or execute when for a language-specific construct.
Yes, verbosity can aid clarity, but only if you actually use it to add clarity. Like using separate variables with clear names to break down a complex expression into its constituent parts.
You can mess up list comprehension clarity just like you can mess up anything in code. That's not the fault of the syntax, however.
For starters, rather than use range(len(example)) and then using the index to get back each value, why not use enumerate()? Use it with starmap() instead of map(), and you can already reduce the amount of code one has to parse here. Each language has idiomatic code patterns, I always try to learn those patterns when working in another programming language. List comprehensions are a Python idiomatic pattern.
19:33
Sure, but with the map thing, you're only using the most bedrock of concepts - function calls. Anyone coming from Java, Scala, C, whatever, will be immediately familiar with what's happening. To be fair I dislike Python in general, so I'm certainly biased, and given to cynicism regarding its expressions.
enumerate sounds like a good call, though, I'll edit that in (and read about starmap).
That's not very helpful then if you want to provide answers here, where there are a lot of Python experts that do know the language.
Fundamental disagreement there - critics of a system have valuable input on a system. That's usually part of my approach to anything in CS - "How can we minimize the damage this can do" as opposed to "How can we get the most out of this." This does all come down to a matter of taste, though, and I don't expect either of us to persuade the other.
I know about the value of criticism, that doesn't mean I have to agree.
I feel like we're both saying we agree to disagree.
So I'm still thinking about it and wanted to pick your brain, if you don't mind:
Looking at the edited answers, I agree that solution = [[i]*len(x) for i, x in enumerate(example)] is better - I only dislike list comprehension syntax when it starts to grow multiple clauses, which that doesn't
But when there's something that starts to get more complicated, like the solutions lacking enumerate - for example solution = [[x for _ in range(len(example[x]))] for x in range(len(example))] - that to me seems worse than the map syntax, as it seems harder to disentangle what binds to what and I'd rather just be dealing with functions
And specifically - and this is what I wanted to ask you about - it looks like that would be a pain to debug. It's pretty trivial to add additional checks, prints, whatever into the map style to investigate if it's not behaving how you want
Sort of the same as how you'll see people objecting to single-line if statements on here, but in this case more bothersome to refactor if you later need to add more
Thoughts? Am I wrong that it's harder to add debugging code to those than it would be to map/lambda style?

last day (15 days later) »