0

What I'm trying to do is turn this

['p', 'y', 't', 'h', 'o', 'n']

into this:

['python']

Ok what if I want to make every list in this list a word

[['f', 'e', 'd'], ['d', 'e', 'f'], ['w', 'o', 'r', 'd']]

into this

[['fed'], ['def'], ['word']]
bagrat
  • 7,158
  • 6
  • 29
  • 47

3 Answers3

2
reduce(lambda a,b:a+b,['p','y','t','h','o','n'])

your probably going to fail this class :/ study hard padwan

y=[['f', 'e', 'd'], ['d', 'e', 'f'], ['w', 'o', 'r', 'd']]
map(lambda x:reduce(lambda a,b:a+b,x),y)
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

As a general solution you can go this way:

lst = [['f', 'e', 'd'], ['d', 'e', 'f'], ['w', 'o', 'r', 'd']]
>>> map(''.join, lst)
['fed', 'def', 'word']

This will basically feed all the elements in the list one-by-one (sublists) into the function provided as the first argument to the map built-in function, which is ''.join is our case. As you can expect, it will provide a new list, that contains strings obtained by joining the sublists.

bagrat
  • 7,158
  • 6
  • 29
  • 47
0

If you want to use only a for loop, list and string methods:

>>> aList = [['f', 'e', 'd'], ['d', 'e', 'f'], ['w', 'o', 'r', 'd']]
>>> newList = []
>>> for X in aList:
...     newList.append("".join(X))
... 
>>> newList
['fed', 'def', 'word']