Jump to content

Adding new strings to list in Python IDLE

Well I'm pretty much a noob in coding. Basically long story short how do I add a string into a list without the string being broken up?

For example:

lst = ["abcde", "fghij", "klmno"]

new_word = "pqrst"

lst += new_word

print(lst)

 

I want lst to be

 ["abcde", "fghij", "klmno", "pqrst"]

. However, when doing this in python it gives me

["abcde", "fghij", "klmno", "p", "q", "r", "s", "t"]

Help?

 

Link to comment
https://linustechtips.com/topic/1018655-adding-new-strings-to-list-in-python-idle/
Share on other sites

Link to post
Share on other sites

1 hour ago, Dredgy said:

Never programmed in Python but fairly sure you have to use append instead of +=

 


list.append(new_word)

 

Edit, think you can also do

 


list = list + ["pqrst"]

 

The last one overrides list so you're better off using the first.

 

If you're keeping data immutable, which you should, then

 


new_list = list + ["string"]

 

Creating a new object or list or every change can be memory intensive for large items so you need to keep that in mind.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×