-3

I've found a way to do what I want which is, But I'm wondering if there's a way I can get this down to one line.

I have a list of list of lists of strings, as compared to a lists of numbers (for which there's an answer: [Sum of list of lists; returns sum list)

Example List:

list = [['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K'],
 ['T=-40F A=10K','T=-15F A=10K','T=59F A=10K','T=98F A=10K','T=120F A=10K']]

Example Output:

['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K', 'T=-40F A=10K', 'T=-15F A=10K', 'T=59F A=10K', 'T=98F A=10K', 'T=120F A=10K']

I can join these with this method:

new = []
for i in [['T=%.0fF A=%.0fK'%(t,a)for t in TEMP] for a in ALT]:
    new = new + i

Anyone got anything?

As for the application im adding a legend to a matplotlib plot

This would be really easy, and an awesome feature with sum(list)

Kevin R.
  • 336
  • 4
  • 14
  • How does your input correspond to your output? And what are you trying to do with `new = []` and `new = new + 1`? – Jon Clements Jul 25 '13 at 14:11
  • @ Inbar Rose: Im not sure its a duplicate due because all those answers are multi line. – Kevin R. Jul 25 '13 at 14:19
  • @hivert : I was looking for something more like what provided Rohit Jain... there's no need to import anything – Kevin R. Jul 25 '13 at 14:27
  • 1
    @CodeMode Importing modules is not something you should be avoiding - the modules are there for a reason. They provide better, fast implementations of these things for you. – Gareth Latty Jul 25 '13 at 14:28
  • @CodeMode: Rohit Jain's code is the same as ``comprehension_flatten`` in http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python. – hivert Jul 25 '13 at 14:31

4 Answers4

3

Using List Comprehension:

>>> my_list = [['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K'], ['T=-40F A=10K','T=-15F A=10K','T=59F A=10K','T=98F A=10K','T=120F A=10K']]
>>>
>>> [y for x in my_list for y in x]
['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K', 'T=-40F A=10K', 'T=-15F A=10K', 'T=59F A=10K', 'T=98F A=10K', 'T=120F A=10K']

And you should not use list as your variable name.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 2
    Note that a list comp will be slower (and less readable) for this task than `itertools.chain.from_iterable()`. – Gareth Latty Jul 25 '13 at 14:28
  • Aware of that, but my lists are fairly small. I'm more for reducing total program size i'd rather not import anything – Kevin R. Jul 25 '13 at 14:31
  • @CodeMode One line of code is not going to matter in terms of size - it will however make your code more readable and efficient. – Gareth Latty Jul 25 '13 at 14:34
  • @CodeMode. Please don't be afraid of importing modules. `itertools` is one such precious module, which you will not want to miss, such useful and efficient functions [it provides](http://docs.python.org/2/library/itertools.html#module-itertools). You should learn using libraries. They are there for some reason. – Rohit Jain Jul 25 '13 at 14:38
  • In either case thank you both! They are both great solutions and really help me with my engineering workload! – Kevin R. Jul 25 '13 at 14:59
3

You want to flatten the iterable - itertools.chain.from_iterable() exists for that very purpose:

>>> data = ...
>>> import itertools
>>> list(itertools.chain.from_iterable(data))
['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K', 'T=-40F A=10K', 'T=-15F A=10K', 'T=59F A=10K', 'T=98F A=10K', 'T=120F A=10K']

It returns an iterator, so you can use list() if you need a list, or just use the iterator.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
2

You can flatten the list using itertools.chain().

>>> testList =[['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K'],
 ['T=-40F A=10K','T=-15F A=10K','T=59F A=10K','T=98F A=10K','T=120F A=10K']]
>>> 
>>> from itertools import chain
>>> chain(*testList)
<itertools.chain object at 0x02B1E910>
>>> list(chain(*testList))
['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K', 'T=-40F A=10K', 'T=-15F A=10K', 'T=59F A=10K', 'T=98F A=10K', 'T=120F A=10K']

OR Use itertools.chain.from_iterable()

>>> list(chain.from_iterable(testList))
['T=-40F A=0K', 'T=-15F A=0K', 'T=59F A=0K', 'T=98F A=0K', 'T=120F A=0K', 'T=-40F A=10K', 'T=-15F A=10K', 'T=59F A=10K', 'T=98F A=10K', 'T=120F A=10K']

P.S - Please don't use list as a variable name, it shadows the builtin.

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
2

Try itertools.chain.fromiterable() -

from itertools import chain
result = list(chain.from_iterable(your_list))
svineet
  • 1,859
  • 1
  • 17
  • 28