python - Array.extend(string) adds every character instead of just the string -


i trying extend element list in python, however, instead of extending string in index 'i' extends every character of string in index 'i'.

for example have list called 'strings' string 'string1' , empty list called 'final_list'.

i want extend first element of 'strings' 'final_list', final_list.extend(strings[0]). instead of 'final_list' end length of 1, corresponding string inserted, list ends length of 7.

if helps, code:

con = connect()     = 0     new_files = []     while < len(files):         info_file = obter_info(con, files[i])         if info_file [5] == 0: #file not processed             new_files.append(files[i])         += 1 

does know how can make work?

the extend method takes iterable argument, unpacks iterable , adds each element individually list upon called. in case, "extending" list string. string iterable. such, string "unpacked" , each character added separately:

>>> d = [] >>> d.extend('hello') >>> print(d) ['h', 'e', 'l', 'l', 'o'] 

if want add 1 element of list list, use append. otherwise, surround string in list , repeat extend:

>>> d = [] >>> d.extend(['hello']) >>> print(d) ['hello'] 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -