0

How can I do this in shorter way:

vmt = ['title', 'designation', 'company', 'address', 'city', 'country']
vmt_copy = []
vmt_copy[:] = ['old-%s' % item for item in vmt]
vmt[len(vmt):] = vmt_copy
vmt = '|'.join(vmt)

Above script outputs:

'title|designation|company|address|city|country|old-title|old-designation|old- company|old-address|old-city|old-country'

2 Answers 2

2
vmt.extend('old-%s' % item for item in vmt[:])

You must use a copy of the list so that you don't create an infinite loop of prepending and adding.

Sign up to request clarification or add additional context in comments.

Comments

1
'|'.join( vmt + map( lambda m: 'old-'+m, vmt) )

Which is essentially what you have.

Edit:

Dont know why this didnt occur to me earlier -

'|'.join(vmt) + '|old-' + '|old-'.join(vmt)

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.