Hi,
I am having the problem in nested list.
Here is the code :

data1= [['Contacts',company.contacts.count() ],
        ['Notes', company.notes.count()],
        ['Met by', ],
        ['Industry', industry],
        ['Financial Investors', '31']]

for item in metlist:
   data1[2].insert(1,item)

Basically I am trying to insert the value for Met by in [ 'Met by', metlist->value]

I am getting list index out of range error.
Can any one help me out.

Thanks

Recommended Answers

All 3 Replies

I think what you're trying to do is:

for item in metlist:
data1[2].append(item)

To see what you did won't work, try typing data1[2]. It gives you ..thus there is only one element, and nothing at position 1.

I think what you're trying to do is:

for item in metlist:
data1[2].append(item)

To see what you did won't work, try typing data1[2]. It gives you ..thus there is only one element, and nothing at position 1.

While I agree with you that he should be using append instead of insert, I don't believe that his error is being caused by there only being one element:

>>> d = ['a']
>>> d.insert(1,'b')
>>> d
['a', 'b']

Something like this might do ...

# converted certain variables to strings for testing only
data1= [['Contacts', 'company.contacts.count()'],
        ['Notes', 'company.notes.count()'],
        ['Met by', ],
        ['Industry', 'industry'],
        ['Financial Investors', '31']]

data2 = []
for item in data1:
    #print item, len(item)  # for testing
    if len(item) < 2:
        item.insert(1, 'missing value')
    data2.append(item)

print data2

"""
my output -->
[['Contacts', 'company.contacts.count()'],
['Notes', 'company.notes.count()'],
['Met by', 'missing value'],
['Industry', 'industry'],
['Financial Investors', '31']]
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.