Kai036
Kai036

Reputation: 310

Map instead of for loop

I have a for loop:

for i in range(len(k_poss)):
    k_poss[i][0] = ' '.join(k_poss[i][0])

Is it possible to use the map function for this instead? I know it could be something like

map(lambda x: ' '.join(x), k_poss)

but how would I go to the index I want in a map?

Upvotes: 2

Views: 291

Answers (1)

576i
576i

Reputation: 8362

You can just loop over k_poss, looping over a range of len of a object is considered non-Pythonic

for k in k_poss:
     k[0] = ' '.join(k[0])

which will make things more readable.

Since you asked, map will also work, but is really not recommended.

See @Jab's example

k_poss = list(map(lambda x: [' '.join(x[0])] + x[1:], iter(k_poss)))

from taken the comment section if you really want to use map, it's better than the attempts I had in here...

Upvotes: 2

Related Questions