Index Error: List index out of range

Posted on 2009-02-24
Last Modified: 2012-05-06
i have a PYTHON script that parses data from an .rdf file. Everything is working great except one field / one section of code that I really could use help on. The data field is called PCIT and it looks like this in the .rdf file>>   ..PCIT:      US20040064316 20040401            <The errror that i receive is :

File "", line 153 in generate
     yyyymmdd = fields[1]
IndexError: list index out of range

here is the code from the file

if ( (dict_key == 'PCIT') and dict_val ):

fields = dict_val.split()

del fields[-1]

yyyymmdd = fields[1]

yyyy          = int(yyyymmdd[0:4])

mm             = int(yyyymmdd[4:6])

if mm < 1: mm = 1

print "YYYY", yyyy, "MM", mm

do = mx.DateTime.Date(yyyy, mm, 1)

fields[1] = do.strftime("%b %y")

dict_val = " ".join(fields)

Open in new window

Question by:Shaggy1
    LVL 28

    Expert Comment

    If the 'US20040064316 20040401' is get in dict_val, then it is split to two-elements list ['US20040064316', '20040401']. Then

    del fields[-1]

    removes the last element and the fields is equal to ['US20040064316']. The only element can be indexed by zero, not by 1.
    LVL 28

    Expert Comment

    Possibly, the dict_val does not contain what you expect. Try to use the following command to clearly visualize the content of a string variable:

    print repr(dict_val)

    The repr() built-in function returns the string that could be used to express the same value as a string literal in your source. (Well, complicated explanation -- easy to understand when you try.)

    Author Comment

    Ok, i used the print repr(dict_val) and value it returned was:

    'DE2914336 19801106'

    This is a different record ref above, but of course the same issue. So, what i need is the date portion of this value which seems to be the 19801106. So instead of
    del fields[-1] i should be using [1] ?
    and how would that affect the rest of the code....
    I am a newbie, but i understand everything you are saying so far....thanks for the help!
    LVL 28

    Accepted Solution

    The fields is the identifier for the Python list which is internally implemented as dynamic array of references to the values (the objects with the values). It can be indexed from zero.

    The .split() method of the built in string returns the list of parts of the string. With default arguments, it splits by spaces and whitespace characters.

    The    fields = 'DE2914336 19801106'.split() returns the list ['DE2914336',  '19801106'] and associates it with name "fields".

    Then fields[0] is equal to 'DE2914336', and the fields[1] is equal to '19801106'. The fields[-1] is a Python syntax for indexing from the end of the list. Here it is the last element of the list.

    The del fields[-1] removes the last element of the list. Then fields has the only element that can be accesed as fields[0]... or also fields[-1] as the last element.

    This way, you may want not to use del or you want to change the index to 0 when accessing the element. Based on decision, you loose the last element or you can use it later.

    You can also do:

         first, second = dict_val.split()

    if you are sure that there are at least two elements. This way the elements are assigned to the variables first and second respectively.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now