Problem with nested list

Thread Solved

Join Date: Dec 2007
Posts: 8
Reputation: laspal is an unknown quantity at this point 
Solved Threads: 0
laspal laspal is offline Offline
Newbie Poster

Problem with nested list

 
0
  #1
Oct 10th, 2008
Hi,
I am having the problem in nested list.
Here is the code :
  1. data1= [['Contacts',company.contacts.count() ],
  2. ['Notes', company.notes.count()],
  3. ['Met by', ],
  4. ['Industry', industry],
  5. ['Financial Investors', '31']]
  6.  
  7. for item in metlist:
  8. 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
Last edited by vegaseat; Oct 12th, 2008 at 11:21 am. Reason: added code tags [code=python] ... [/code]
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1
Reputation: dittmarw is an unknown quantity at this point 
Solved Threads: 1
dittmarw dittmarw is offline Offline
Newbie Poster

Re: Problem with nested list

 
0
  #2
Oct 10th, 2008
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 ['Met by']..thus there is only one element, and nothing at position 1.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,099
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 287
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Problem with nested list

 
0
  #3
Oct 10th, 2008
Originally Posted by dittmarw View Post
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 ['Met by']..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:
  1. >>> d = ['a']
  2. >>> d.insert(1,'b')
  3. >>> d
  4. ['a', 'b']
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,666
Reputation: vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light vegaseat is a glorious beacon of light 
Solved Threads: 1091
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with nested list

 
0
  #4
Oct 12th, 2008
Something like this might do ...
  1. # converted certain variables to strings for testing only
  2. data1= [['Contacts', 'company.contacts.count()'],
  3. ['Notes', 'company.notes.count()'],
  4. ['Met by', ],
  5. ['Industry', 'industry'],
  6. ['Financial Investors', '31']]
  7.  
  8. data2 = []
  9. for item in data1:
  10. #print item, len(item) # for testing
  11. if len(item) < 2:
  12. item.insert(1, 'missing value')
  13. data2.append(item)
  14.  
  15. print data2
  16.  
  17. """
  18. my output -->
  19. [['Contacts', 'company.contacts.count()'],
  20. ['Notes', 'company.notes.count()'],
  21. ['Met by', 'missing value'],
  22. ['Industry', 'industry'],
  23. ['Financial Investors', '31']]
  24. """
Last edited by vegaseat; Oct 12th, 2008 at 11:34 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 671 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC