#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2

    Save text data from JSON as a list


    Hi, I'm struggling on how to save text which I loaded from JSON to a variable as a list.

    I loaded data like this:

    Code:
    l =[]
    
    for i in range (10):
         l = results[i]['message']
    The problem is when I check "l" it will show me only the last data of i which is 10. That means it print results[10]['message'] for me which is only 1 value (I need 10 of them)

    Can anybody help me solve this problem, thank you so much.

    P.S. Sorry that my question is at beginner level, but I have no idea now to deal with this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    Because your code loops through the check 10 times, by the time it's finished, the "l" variable is no longer a list, it's literally the value "results[10]['message']"

    Try append:

    Code:
    l =[]
    for i in range (10):
         l.append(results[i]['message'])
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Oh Thank you so much sir, it works

IMN logo majestic logo threadwatch logo seochat tools logo