Title: Generator-expression, getting the same results of using append or extend in a for-loop
Tags: python;for-loop;append;extend;generator-expression
Question: usingAppend = []; usingExtend = []; usingLC = []
d = {'pKey_b': 'vb1', 'pKey_e': 've1', 'pKey_c': 'vc1', 'pKey_a': 'va1', 'pKey_d': 'vd1'}
Using append in the for-loop gives a list with sublists
for k, v in sorted(d.iteritems()):
usingAppend.append([k, v])
print '\n', usingAppend
['pKey_a', 'va1'],
['pKey_b', 'vb1'],
['pKey_c', 'vc1'],
['pKey_d', 'vd1'],
['pKey_e', 've1']
Using extend in the for-loop gives a single list
for k, v in sorted(d.iteritems()):
usingExtend.extend((k, v))
print '\n', usingExtend
['pKey_a', 'va1', 'pKey_b', 'vb1', 'pKey_c', 'vc1', 'pKey_d', 'vd1', 'pKey_e', 've1']
Using this generator expression gives the same results as using append in a for loop, a list with sublists
usingLC = sorted([k, v] for k, v in d.iteritems())
print '\n', usingLC
['pKey_a', 'va1'],
['pKey_b', 'vb1'],
['pKey_c', 'vc1'],
['pKey_d', 'vd1'],
['pKey_e', 've1']
My question is, is there a way to set up the generator expression to give the same results as using extend in a for-loop
Comment: I changed my post to say generator expression along with the tag, thanks for pointing that out.

Comment: You have a generator expression, not a list comprehension. The same principles apply though.

Comment: @Michael you're allowed to [edit] your own posts :)

Here is the accepted answer: You use double looping:
[i for k, v in sorted(d.iteritems()) for i in (k, v)]
[i for item in sorted(d.iteritems()) for i in item]
or itertools.chain.from_iterable():
from itertools import chain
list(chain.from_iterable(item for item in sorted(d.iteritems())))
although you could just use:
list(chain.from_iterable(sorted(d.iteritems())))
in these cases.
&gt;&gt;&gt; d = {'pKey_b': 'vb1', 'pKey_e': 've1', 'pKey_c': 'vc1', 'pKey_a': 'va1', 'pKey_d': 'vd1'}
&gt;&gt;&gt; [i for k, v in sorted(d.iteritems()) for i in (k, v)]
['pKey_a', 'va1', 'pKey_b', 'vb1', 'pKey_c', 'vc1', 'pKey_d', 'vd1', 'pKey_e', 've1']
&gt;&gt;&gt; from itertools import chain
&gt;&gt;&gt; list(chain.from_iterable(item for item in sorted(d.iteritems())))
['pKey_a', 'va1', 'pKey_b', 'vb1', 'pKey_c', 'vc1', 'pKey_d', 'vd1', 'pKey_e', 've1']
&gt;&gt;&gt; list(chain.from_iterable(sorted(d.iteritems())))
['pKey_a', 'va1', 'pKey_b', 'vb1', 'pKey_c', 'vc1', 'pKey_d', 'vd1', 'pKey_e', 've1']
Comment for this answer: I like the double-looping solution, it's shorter than chaining solution, and easier to understand than the other two. Your solution gets marked as the answer.
Comment for this answer: @SvenMarnach: true, I'll adjust
Comment for this answer: @MichaelSwartz: I'd only use the `chain.from_iterable()` if I wanted the result to be an iterable still (e.g. not call `list()` on it but just iterate over the object instead).


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/07/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><for-loop><append><extend><generator-expression>', 'Title': 'Generator-expression, getting the same results of using append or extend in a for-loop', 'ViewCount': 88, 'id': 22250468, 'user_AboutMe': '', 'user_AccountId': 1593603.0, 'user_DisplayName': 'Michael Swartz', 'user_Reputation': 820.0, 'user_Views': 215.0, 'user_id': 1476365.0}}