last day (15 days later) » 

5:10 PM
Hey, I did not want to just paste a question on the lounge
So, I thought I'd give you the link
2
A: Python Function Definition - Debug please

Games BrainiacIf you want to split a string, you can do the following: >>> string = "ABCDEFG" >>> list(string) ['A', 'B', 'C', 'D', 'E', 'F', 'G'] >>> If you wanted to up a ^ infront of the capital letters: >>> my_string = "ABCDEFG" >>> [('^', char) for char in my_string.lower()] [('^', 'a'), ('^', 'b'), ...

Take a look at the last code example.
And yes I know, my answer sucks :(
So ideally, I needed to use 2 for loops because I simple wanted to append 2 things at the same time, its wasteful because I need to use 2 for loops, where there should just be one.
 
That's not really a problem
map is pretty much always 1:1 in terms of element count
 
But still, there should be a way of doing something as trivial as this.
I mean [('^', char) for char in mystring] would give you a list of tuples.
 
def prepend_all(it, c):
    for x in it:
        yield c
        yield x
Bad names, whatever
 
ahh now it makes more sense :P
@CatPlusPlus Yes, this can be done, but list comprehensions does not provide a simple way to do this, and thats a shame.
You can also use itertools.chain
 
What do you want to chain here?
@GamesBrainiac They never do, it's not a Python issue
 
5:18 PM
(list(itertools.chain.from_iterable(('^', x) for x in mystring.lower())))
@CatPlusPlus eh?
 
@GamesBrainiac That's just unnecessarily complex
 
@CatPlusPlus It is but it can be done.
 
(And still two loops, you just hide one)
 
The best method I've found is using 2 for loops.
 
Yes, that's how you flatten lists
 
5:19 PM
Ahh
Exactly, but I cant add two things at the same time.
 
You're basically mapping [a] to [[a]] and then flatten it back to [a]
 
@CatPlusPlus Yea, and thats wasteful, when one of the things I'm trying to add is just a character.
 
@GamesBrainiac What's wasteful?
 
@CatPlusPlus The second for loop and the flattening of the list. Why do you even need to flatten, you should be able to add to things to the list at once.
I've tried using the splat operator * but that dsnt work.
gives me a syntax error.
 
@GamesBrainiac Because that's how list comps work
In any language
 
5:21 PM
Ahh
 
You don't have to use list comps for absolutely every list operation
 
I know, but it looks cool :P
Thanks for your time Cat. I humbly appreciate it.
I know you're busy, so I won't waste any more of your time.
 

last day (15 days later) »