I want to append every value of list1 in every value of list2, so that this:

let list1 = ['one', 'two', 'three']
let list2 = [' wan', ' tu', ' tri']

will become like this:

:echo result
['one wan', 'two tu', 'three tri']

How could this be done?


I don't like loops -- they are slow and a pain to debug as there is no "continue until the end of the current loop"

:echo map(range(len(list1)), 'list1[v:val].list2[v:val]')

Note: a zip() function is missing, but it can be emulated if we produce a range of indexes into both arrays. We could also simply transform a copy of list1 and use v:key (Vim 7.2.???) for looping over indices, but I prefer range(len(list1))


Because you want to ":echo result", my way would be straightforward: :let result = list1 :map(result, 'result[v:key].list2[v:key]') :echo result

(Because map() does in-place replacement, it requires a little trick for one-liner, like @LucHermitte's answer.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.