Kimmy
Kimmy

Reputation: 4049

How to get variable from a list field

Hi i have a variable that prints out the following:

data=[('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b')]

I am using python i was wondering how to i extract the content of location_name for example from this list, i have tried doing the following:

data[0]// this prints out ('location_name', u'b') 

What i want is to get the content of location_name so in this case i get b

Many thanks

Upvotes: 0

Views: 155

Answers (3)

ovrwngtvity
ovrwngtvity

Reputation: 4419

You can acess a data using index. Like test=[(1, 2, 3), (4, 5, 6), (5, 6, 7)]

teste[0] # will access the first element that is (1, 2, 3)
'(1, 2, 3)'
teste[0][0] # will access the element (1, 2, 3) then the second index [0] will access 1
'1'

As inner as the element is, more index you will need.

Upvotes: 0

JonLeslieHarding
JonLeslieHarding

Reputation: 135

You have a list of tuples. Therefore, to print that value you would want simply to do:

print data[0][1]

Upvotes: 1

John
John

Reputation: 13699

You can do it like this

data=[('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b')]
d = dict(data)

print d["location_name"]

Upvotes: 3

Related Questions