473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

simple problem with lists I am just forgetting


Lets say we have this list:

funlist = ['a', 'b', 'c']

and lets say I do this:

if funlist[4]:
print funlist[4]

I will get the exception "list index out of range"

How can I test if the list item is empty without getting that exception?
--
View this message in context: http://www.nabble.com/simple-problem...p18762181.html
Sent from the Python - python-list mailing list archive at Nabble.com.

Jul 31 '08 #1
2 932
Alexnb:
How can I test if the list item is empty without getting that exception?
In Python such list cell isn't empty, it's absent. So you can use
len(somelist) to see how much long the list is before accessing its
items. Often you can iterate on the list with a for, so you don't need
to care of the len().

Bye,
bearophile
Jul 31 '08 #2
On Jul 31, 2:51*pm, Alexnb <alexnbr...@gmail.comwrote:
Lets say we have this list:

funlist = ['a', 'b', 'c']

and lets say I do this:

if funlist[4]:
* * print funlist[4]

I will get the exception "list index out of range"

How can I test if the list item is empty without getting that exception?
Try using a slice. Slices are ignorant of list index validity.

funlist = list("abc") # too lazy to type ['a', 'b', 'c']
if funlist[4:]:
print funlist[4]

Voila! No exception!

-- Paul
Jul 31 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
15
by: Ashot | last post by:
This is sort of both Python and Vim related (which is why I've posted to both newsgroups). Python related: ---------------------- I have been frustrated for quite some time with a lack of a...
4
by: Tom Johnson | last post by:
Hi all, I cant believe that Im stuck on such simple code, but I am so... Im trying to open a text file for writing but can never get the file to initially open. Heres what Im having trouble with....
0
by: Steven Roussey | last post by:
> Executing just the search on the word table, with no joins to the > table with the dates, is still slow: Then it is not worth while to focus on anything else until you fix that. Are the...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
1
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
7
by: Zethex | last post by:
Im a bit new to python. Anyway working on a little project of mine and i have nested lists ie Answer = , ] and so forth.., Anyway the amount of ] do increase over time. Im just wondering...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.