dojogeorge
dojogeorge

Reputation: 1704

Python Remove 2nd item from List within List

I have a list in the following format:

[[[u'Hot Dogs', u'hotdog'], [u'Food Stands', u'foodstands']], [[u'Scandinavian',       u'scandinavian'], [u'Breakfast & Brunch', u'breakfast_brunch'], [u'Coffee & Tea', u'coffee']],    [[u'Burgers', u'burgers']]]

I would like to remove the first item from each list (it is just a duplicate of the 2nd) and then return a simple list of these individual tags, rather than being enclosed in mutiple []. How would I go about doing this?

Edit: I would like to return a list of lists with each line representing the 2nd tag in each list as mentioned below

Upvotes: 0

Views: 176

Answers (2)

scriptmonster
scriptmonster

Reputation: 2771

I think what you want is this:

rawList = [[[u'Hot Dogs', u'hotdog'], [u'Food Stands', u'foodstands']], [[u'Scandinavian',       u'scandinavian'], [u'Breakfast & Brunch', u'breakfast_brunch'], [u'Coffee & Tea', u'coffee']],    [[u'Burgers', u'burgers']]]
finalList = []

for l in rawList:
    finalList.append([i[0] for i in l])

Output will be as follows:

[[u'Hot Dogs', u'Food Stands'], [u'Scandinavian', u'Breakfast & Brunch', u'Coffee & Tea'], [u'Burgers']]

Upvotes: 2

Marc Tudurí
Marc Tudurí

Reputation: 1949

You can do in easy way using list comprehesion:

[y[1] for x in l for y in x]

where l is the list that you specified.

Upvotes: 3

Related Questions