The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Beginner Q: for loop syntax clarification
Discuss Beginner Q: for loop syntax clarification in the Python Programming forum on Dev Shed. Beginner Q: for loop syntax clarification Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 8th, 2013, 07:03 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 19 m 26 sec
Reputation 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
|

February 8th, 2013, 08:28 AM
|
 |
Lord of the Dance
|
|
|
|
|
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.
|

February 8th, 2013, 09:07 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 19 m 26 sec
Reputation 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?
Quote: | Originally Posted by MrFujin Take a look at this page:
xx
This is what the code connects to and retrieve data from. |
|

February 8th, 2013, 11:31 AM
|
 |
Contributing User
|
|
|
|
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!
|

February 8th, 2013, 11:46 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 3
Time spent in forums: 19 m 26 sec
Reputation 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! :-)
Quote: | 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' |
|

February 8th, 2013, 12:32 PM
|
 |
Contributing User
|
|
|
|
|
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.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : February 8th, 2013 at 01:04 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|