0

For example:

list = [[11,2,3,5],[5,3,74,1,90]]

returns the same thing, only everything is a str instead of an int. I want to be able to use .join on them. Thanks!

Aei
  • 677
  • 6
  • 11
  • 17
  • 2
    As an aside don't call your list `list`, as this will clash with the python keyword. – DaveP Nov 09 '12 at 05:11
  • http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python and str the "item" in the list comprehension. – KevinL Nov 09 '12 at 05:12

2 Answers2

5

If you only ever go 2 lists deep:

>>> l = [[11, 2, 3, 5], [5, 3, 74, 1, 90]]
>>> [[str(j) for j in i] for i in l]
[['11', '2', '3', '5'], ['5', '3', '74', '1', '90']]
Tim
  • 11,710
  • 4
  • 42
  • 43
  • This was exactly what I needed. I had come up with something similar but it was more difficult to implement without this list comprehension. Thanks! – Aei Nov 09 '12 at 05:24
3

I'd use a list-comp and map for this one:

[ map(str,x) for x in lst ]

But I suppose py3.x would need an addition list in there (yuck).

[ list(map(str,x)) for x in lst ]

As a side note, you can't use join on this list we return anyway. I'm guessing you want to do something like this:

for x in lst:
   print ("".join(x))

If that's the case, you can forgo the conversion all together and just do it when you're joining:

for x in lst:
   print ("".join(str(item) for item in x))
mgilson
  • 300,191
  • 65
  • 633
  • 696