1

I have multiple strings printing from one variable 'words' in a for loop from a json file, then i get these names.

with open('list.json', 'r') as i:
    names = json.load(i)

for a in names:
  words = (a['id']['name'])
  print words

'Vincent'
'Jackson'
'Marie'
'Mold'
'Gary'
'Sameul'

I want to put all of these names into an array, and keep it inside the variable 'words'.

words = ['Vincent','Jackson','Marie','Mold','Gary','Sameul']
2
  • 1
    Initialize a empty list and append a['id']['name'] in each iteration. Commented Dec 28, 2019 at 7:52
  • wow that was fast, thanks! Commented Dec 28, 2019 at 7:53

3 Answers 3

4

Python lists are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time. If you need to shrink and grow your list time-efficiently and without hassle, they are the way to go.

You can store them into a list.

words = []
for a in names:
  words.append(a['id']['name'])

Or

You can use list comprehensions. This will replace your for loop entirely:

words = [a['id']['name'] for a in names]

I want to put all of these names into an array, and keep it inside the variable 'words'.

I am assuming you are flexible to use any data structure in Python and by array, you just mean a sequence data type. However, I am adding some information on array for the sake of completeness.

An array is a data structure that stores values of same data type. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to same data type.

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

Comments

1
with open('list.json', 'r') as i:
    names = json.load(i)

for a in names:
  words = (a['id']['name'])

wordList = []
for word in words:
    wordList.append(word)

Comments

1

If you are new to python, there's a List data structure you might wanna check out. This List can hold arbitrary data and you can append element in this list.

For your problem, before iterating the names you read from the json file, initialize a list.

list_that_holds_names = []

And then, during each iteration, append the element from the names

for a in names:
  words = (a['id']['name'])
  list_that_holds_names.append(words)

Now this list_that_holds_names list contains all the names. If you print the list, it will geberate the expected output.

But please, do Google stuff or try learning first from Google before posting here.

1 Comment

yeah, sorry, i did try to google but i did not get many results so i came here, i did not know what to google for, thanks tho~!

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.