0

Using Python, I have a complex list that I am trying to pull various information from. When I print the entire list in question, below is what I get.

{'previous': None, 
 'results': [
             {'account': 'https:REDACTED', 
                  'url': 'https:REDACTED', 
               'amount': '4.32', 
         'payable_date': '2018-03-08', 
           'instrument': 'https:REDACTED', 
                 'rate': '1.4400000000', 
          'record_date': '2018-03-05', 
             'position': '3.0000', 
          'withholding': '0.00', 
                   'id': 'REDACTED', 
              'paid_at': None}, 

       {'account': 'REDACTED', 'url': 'https:REDACTED',

...AND SO ON... }

This code -

for x in list:
       print(x)

prints - 'previous' 'results' (which you can see are both to the left side of their respective colons (:))

I want to be able to print the information on the other side of the colon (:), so:

'results': [{'account': 'https:REDACTED', 'url': 'https:REDACTED',... etc.

printing x[0] results in 'r' (first letter in 'results').

How do I get to the information on the right side of the 'results' colon (:)? Thanks

user8016906
  • 101
  • 1
  • 7
  • We don't have enough information to be useful to you .. You aren't giving 1) a context or 2) a programming language! – Zak Mar 08 '18 at 19:10
  • Oops, its Python. And I am sorry about the lack of information, I've never seen a list/array/whatever like this before so I don't even know what to call it! – user8016906 Mar 08 '18 at 19:13
  • Looks like JSON - see [here](https://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it) – dgg Mar 08 '18 at 19:27
  • That JSON clue helped, I guess JSON is very similar to a dictionary. That's what this is. Thanks. – user8016906 Mar 08 '18 at 19:44

1 Answers1

0

I'm not sure what a 'complex list' is, but what you're using is a python dictionary. The loop you need is: for x in list: print(x, list[x]) When looping through this dictionary, 'x' is the 'key' in the dictionary (it works like the index does for a list), to get the data contained in this entry, you need to extract it from the dictionary manually, the loop doesn't do it automatically.

Chris
  • 31
  • 4