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

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Beginner Q: for loop syntax clarification


    I am a complete newbie to Python and need to work with Twitter data using Python on an immediate basis. Can anyone help me understand the last line of the following code? or any pointers to look online. I see there's a for loop but not sure how the iteration works as its different from for specification I came across in Py sofar.

    Code:
    import twitter # Tell Python to use the twitter package 
    
    twitter_api = twitter.Twitter(domain="api.twitter.com",api_version='1') # create a variable named twitter_api of class "twitter.Twitter()" 
    
    WORLD_WOE_ID = 1 # The Yahoo! Where On Earth ID for the 
    entire world 
    
    world_trends = twitter_api.trends._(WORLD_WOE_ID) # get back a callable 
    
    [trend for trend in world_trends()[0]['trends']]
    Thanks
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,540
    Rep Power
    1906
    Take a look at this page:
    https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid

    This is what the code connects to and retrieve data from.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Thanks for reply!
    I understand the API, but I wanted to know what kind of loop is it in the last line of code I posted in my first post with statement within square brackets []. what is it called?

    Originally Posted by MrFujin
    Take a look at this page:
    xx
    This is what the code connects to and retrieve data from.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    example
    Code:
    def world_trends():
        LIST = []
        LIST.append( dict( kingsPi = ['22', '/', '7'], trends = (2, 7, 1, 8, 2, )))
        return LIST
    
    print('output of world_trends()')
    print(world_trends())
    
    print('')
    
    print('nasty last line of post')
    print([trend for trend in world_trends()[0]['trends']])
    
    print('')
    
    print('simplification of nasty last line of post')
    print(list(world_trends()[0]['trends']))

    1) In your code
    [trend for trend in world_trends()[0]['trends']]
    is not stored. It would take a contrived example to make this statement have any sort of noticeable side effect.

    2) Why not just use world_trends()[0]['trends'] instead? Not all iterables are lists, indeed
    dict(a=3) is iterable but
    dict(a=3)[0] looks up the value at key 0 not item 0.
    list(dict(a=3))[0] == 'a'
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Very clear explanation.. I also understand now why this code did not show any result in my pyCharm IDE output window, while it worked fine on python interpreter console.

    thank you very much! :-)

    Originally Posted by b49P23TIvg
    example
    Code:
    def world_trends():
        LIST = []
        LIST.append( dict( kingsPi = ['22', '/', '7'], trends = (2, 7, 1, 8, 2, )))
        return LIST
    
    print('output of world_trends()')
    print(world_trends())
    
    print('')
    
    print('nasty last line of post')
    print([trend for trend in world_trends()[0]['trends']])
    
    print('')
    
    print('simplification of nasty last line of post')
    print(list(world_trends()[0]['trends']))

    1) In your code
    [trend for trend in world_trends()[0]['trends']]
    is not stored. It would take a contrived example to make this statement have any sort of noticeable side effect.

    2) Why not just use world_trends()[0]['trends'] instead? Not all iterables are lists, indeed
    dict(a=3) is iterable but
    dict(a=3)[0] looks up the value at key 0 not item 0.
    list(dict(a=3))[0] == 'a'
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Trying to run your code, all I get is ...
    AttributeError: 'module' object has no attribute 'Twitter'

    I think you get a "JavaScript Object Notation" object. Looks like a Python dictionary.
    Last edited by Dietrich; February 8th, 2013 at 01:04 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo