dru
dru

Reputation: 708

Append a string to a list of unicode strings

What I've tried:

>> abcd = [u'abcd']
>> abcd_ef = abcd + 'ef'
>> abcd_ef

[u'abcd', 'e', 'f']

What I'd like:

>> abcd = [u'abcd']
>> abcd_ef = **MAGIC ???**
>> abcd_ef

[u'abcd', 'ef']

Hopefully I made that clear enough!

Upvotes: 1

Views: 2951

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121962

Make it a list:

>>> abcd = [u'abcd']
>>> abcd_ef = abcd + ['ef']
>>> abcd_ef
[u'abcd', 'ef']

otherwise the list adds each element (e.g. each character) of the string separately.

Alternatively, you can call .append() on abcd and modify that list in-place:

>>> abcd = [u'abcd']
>>> abcd.append('ef')
>>> abcd
[u'abcd', 'ef']

This is all standard python list manipulation and is independent of the contents; it doesn't matter if there are unicode objects or custom objects in that list.

Upvotes: 4

Related Questions