1

I'm new to python. I tried to store bunch of strings to an array and at the end print out the array, however it print out as a long list of characters. here is my code:

user_with_no_records = [""]
for user_test_docs in json_data['results']:
   ... do something here ...
   user_with_no_records.extend(user_test_docs['userId'].replace("'", '"'))
...
pprint(user_with_no_records)

instead of print out :

"1234-4a20-47c0-b23c-a35a", "53dd-4120-4249-b4f6-ebe2"

it print out

"1","2","3","4","-","a","2","0"....
user468587
  • 4,799
  • 24
  • 67
  • 124

1 Answers1

3

a.extend(b) is for extending list a by concatenating another sequence b onto it. When b is a string, and you force it to be interpreted as a sequence, it is interpreted as a sequence of individual characters. A simple example of this is:

>>> b = 'Hello'
>>> list(b)
['H', 'e', 'l', 'l', 'o']

Instead, you clearly want to do a.append(b), i.e. insert the entire string b as a single new item at the end of a.

jez
  • 14,867
  • 5
  • 37
  • 64