0

Suppose I have a list of lists say

A = [[1,1,1,1],[2,2,2,2]]

and I want to create two strings from that to be

'1111'

'2222'

How would we do this in python?

2 Answers2

5

Maybe list comprehension:

>>> A = [[1,1,1,1],[2,2,2,2]]
>>> l=[''.join(map(str,i)) for i in A]
>>> l
['1111', '2222']
>>> 

Now you've got it.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Hey I tried this method but I am getting odd results. I am have a list of lists like this[[1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 1, 2], [2, 2, 5, 5, 4, 3, 0, 0, 3, 3, 2, 2]] and when I apply your code I get this['100000002.02.01.02.0', '22.05.05.04.03.0003.03.02.02.0', '000000009.010.010.00', '000000000.011.010.09.0', '0000001.01.011.01.01.0', '12.02.01.01.01.01.01.011.01.01.0', '0000000011.011.000', –  Nov 21 '18 at 00:40
  • 1
    yea I know I am getting the same but when I try to do the same thing on my larger list of lists I get those weird results, any idea how to fix it? –  Nov 21 '18 at 00:43
  • @Snorrlaxxx Even tho it doesn't reproduce that two me with large lists, but you can try: `l=[''.join(map(str,i)).replace('.','') for i in A]`. – U13-Forward Nov 21 '18 at 00:45
1

This is pretty easily done using join and a list comprehension.

A = [[1,1,1,1],[2,2,2,2]]
a_strings = [''.join(map(str, sub_list)) for sublist in A]

See, join() takes a list of strings and makes a string concatenating all the substrings and the list comprehension I used just loops through them all. Above I combined the 2 together.


On a second thought

map() is actually deemed more efficient (when not using lambda.. etc) and for SOME more readable. I'll just add an approach using map instead of a comprehension.

a_strings = map(''.join(), map(str, A))

This first takes the inner map and makes all the ints > strs then joins all the strs together for every sub-list.

Hopefully this makes things a bit more chewable for ya, each method is close to equivalent such that for this case you could consider them style choices.

Jab
  • 26,853
  • 21
  • 75
  • 114
  • There's an error: `TypeError: sequence item 0: expected str instance, int found` – U13-Forward Nov 21 '18 at 00:20
  • Now it's mine!! – U13-Forward Nov 21 '18 at 00:22
  • 1
    @U9-Forward Sorry, lol I didn't mean to steal from you haha I had all the same logic I just was ignorant to the fact that they were all integers. I still would have used the same approach. – Jab Nov 21 '18 at 00:31
  • 1
    lol well copying was faster than typing it myself, I just like explaining in more detail when I think notice a beginner ;) Either way great minds think alike, I did test my code cuz I thought I was having a stroke BTW XD – Jab Nov 21 '18 at 00:36
  • 1
    We do both enjoy the early pounce on a fast answer, now wanna team up and defeat the rebel alliance with me? This way it's not stealing if you're on the same team **Edit** oops, getting off track... back to F5 on `python` tag – Jab Nov 21 '18 at 00:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184048/discussion-between-jaba-and-u9-forward). – Jab Nov 21 '18 at 17:20