0

I have a nested list main_category, each nested list is a unicode string of business names. The first five lines of the nested lists are below:

[[u'Medical Centers', u'Health and Medical'],
[u'Massage', u'Beauty and Spas'],
[u'Tattoo', u'Beauty and Spas'],
[u'Music & DVDs', u'Books, Mags, Music and Video', u'Shopping'],
[u'Food', u'Coffee & Tea']]

So I want to get the first element of every list, and I have tried list comprehension, zip, but nothing works.

new_cate = [d[0] for d in main_category]
lst = zip(*main_category)[0]

But all of them give me

IndexErrorTraceback (most recent call last)
<ipython-input-49-4a397c8e62fd> in <module>()
----> 1 lst = zip(*main_category)[0]
IndexError: list index out of range

I really don't know what is wrong with this. So could anyone help? Thanks so much!

Parker
  • 193
  • 2
  • 3
  • 14
  • Could you verify the code which you execute? copy past from your question works fine for me ['Medical Centers', 'Massage', 'Tattoo', 'Music & DVDs', 'Food'] and ('Medical Centers', 'Massage', 'Tattoo', 'Music & DVDs', 'Food') – Oleksandr Dashkov Nov 10 '16 at 15:21
  • Your full list contains an empty sublist `[]` somewhere. Do confirm that. – Moses Koledoye Nov 10 '16 at 15:24
  • 1
    Possible duplicate of [Extract first item of each sublist in python](http://stackoverflow.com/questions/25050311/extract-first-item-of-each-sublist-in-python) – Arthur Zopellaro Nov 10 '16 at 15:26
  • To Dashkov, this is just the first five lines of my list, and they should work fine. The problem comes from the rest of them – Parker Nov 10 '16 at 15:30
  • To Moses, this full list is very large and I can't really tell where the empty sublist is. If I can't locate this empty sublist, what should I do – Parker Nov 10 '16 at 15:31

2 Answers2

0

The error indicates one/some of the sublists in the full list are empty lists. You need to properly handle that. You can put a ternary operator in the list comprehension to substitute a default value when the list empty is and index the first item when it isn't:

default = ''
new_cate = [d[0] if d else default for d in main_category]
#                ^^^^-> test if list is truthy

You can also replicate this fix for zip by using it's itertools variant izip_longest which allows you to set a fillvalue:

from itertools import izip_longest

default = ''
lst = list(izip_longest(*main_category, fillvalue=default))[0]
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
-1

So you have a list of lists.

for content in matrix:

At each iteration content will return a full list. [u'Medical Centers', u'Health and Medical'] for example.

If you print(content[0]), you will get the first value of the current list, which would be u'Medical Centers'.

If there's a list without content in the matrix, print(content[0]) would raise IndexError, so you need to check if the current list is not None with if content:.


matrix = [[u'Medical Centers', u'Health and Medical'],
[u'Massage', u'Beauty and Spas'],
[u'Tattoo', u'Beauty and Spas'],
[u'Music & DVDs', u'Books, Mags, Music and Video', u'Shopping'],
[u'Food', u'Coffee & Tea']]

for content in matrix:
    if content:
        print(content[0])

>>> Medical Centers
>>> Massage
>>> Tattoo
>>> Music & DVDs
>>> Food
Arthur Zopellaro
  • 158
  • 1
  • 12