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!

2
  • 2
    As an aside don't call your list list, as this will clash with the python keyword. Commented Nov 9, 2012 at 5:11
  • stackoverflow.com/questions/952914/… and str the "item" in the list comprehension. Commented Nov 9, 2012 at 5:12

2 Answers 2

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']]
Sign up to request clarification or add additional context in comments.

1 Comment

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!
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.