I'm trying to use a for loop to iterate through the first element of a list of tuples.
for i in link_list:
print 'http://www.newyorksocialdiary.com%s' % link_list[i][0]
However, I get this error:
TypeError Traceback (most recent call last)
<ipython-input-72-8c0e1be937a4> in <module>()
1 for i in link_list:
----> 2 print 'http://www.newyorksocialdiary.com%s' % link_list[i][0]
TypeError: list indices must be integers, not tuple
How can I iterate through the list of tuples and print only the first element, such as:
'http://www.newyorksocialdiary.com/party-pictures/2014/the-thanksgiving-day-parade-from-the-ground-up'
'http://www.newyorksocialdiary.com/party-pictures/2014/gala-guests'
If it helps, this is what link_list looks like:
[('/party-pictures/2014/the-thanksgiving-day-parade-from-the-ground-up',
datetime.datetime(2014, 12, 1, 0, 0)),
('/party-pictures/2014/gala-guests', datetime.datetime(2014, 11, 24, 0, 0)),
('/party-pictures/2014/equal-justice', datetime.datetime(2014, 11, 20, 0, 0)),
('/party-pictures/2014/celebrating-the-treasures',
datetime.datetime(2014, 11, 18, 0, 0)),
('/party-pictures/2014/associates-and-friends',
datetime.datetime(2014, 11, 17, 0, 0))]
% i[0]becauseforloops by default iterate over the container elements, not the indices.link_list = [(1, "foo"), (2, "bar")...]Also, keep in mind that when you dofor i in list(), you get (ini) items in the list, not indexes (maybe you wanted to do something likefor i in range(len(link_list))?