By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,378 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

return the last item in a list

P: n/a

I've googled for the above and get way too many hits..

I'm looking for an 'easy' way to have the last item in a list returned.

I've thought about

list[len(list)-1]

but thought there would be a more gracefull way.

Jul 18 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
On 30 Mar 2005 10:48:17 -0700, rumours say that David Bear
<Da********@asu.edu> might have written:
I've googled for the above and get way too many hits..

I'm looking for an 'easy' way to have the last item in a list returned.

I've thought about

list[len(list)-1]

but thought there would be a more gracefull way.


There is.

alist[-1]

Did you read the tutorial? This is referenced in "3. An Informal
Introduction to Python".
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #2

P: n/a
[David Bear]
I'm looking for an 'easy' way to have the last item in a list returned.


Try mylist.pop() or mylist[-1].
Raymond Hettinger
Jul 18 '05 #3

P: n/a
Raymond Hettinger wrote:
I'm looking for an 'easy' way to have the last item in a list returned.


Try mylist.pop() or mylist[-1].


Note that list.pop also removes the last element from the list in the
process.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Granted that I must die, how shall I live?
-- Michael Novak
Jul 18 '05 #4

P: n/a
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman
Jul 18 '05 #5

P: n/a
What OS? Linux? Solaris?

J

Roman Yakovenko wrote:
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman

Jul 18 '05 #6

P: n/a
On Mar 31, 2005 9:20 AM, John Abel <jo*******@pa.press.net> wrote:
What OS? Linux? Solaris?
Does it matter? If so, please explain why ( lack of knowledge )
I am using Linux ( Debian Surge )

Thanks

J

Roman Yakovenko wrote:
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman

Jul 18 '05 #7

P: n/a
With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.

HTH

J

Roman Yakovenko wrote:
On Mar 31, 2005 9:20 AM, John Abel <jo*******@pa.press.net> wrote:

What OS? Linux? Solaris?


Does it matter? If so, please explain why ( lack of knowledge )
I am using Linux ( Debian Surge )

Thanks

J

Roman Yakovenko wrote:
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman



--
*John Abel
Senior Unix Administrator*
PA News Limited
www.pa.press.net <http://www.pa.press.net>
E-Mail address: jo*******@pa.press.net <mailto:jo*******@pa.press.net>
Telephone Number : 01430 455553
Fax Number : 0870 1240192
Mobile Number : 07971 611356
The Bishop's Manor, Market Place, Howden, DN14 7BL
PA News Limited, 292 Vauxhall Bridge Road, London SW1V 1AE. Registered
in England No. 3891053.
Jul 18 '05 #8

P: n/a
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

On Mar 31, 2005 9:35 AM, John Abel <jo*******@pa.press.net> wrote:
With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.
I think I should have permissions to do it. (more over users of my
scripts should have permissions )
HTH

J

Roman Yakovenko wrote:
On Mar 31, 2005 9:20 AM, John Abel <jo*******@pa.press.net> wrote:

What OS? Linux? Solaris?


Does it matter? If so, please explain why ( lack of knowledge )
I am using Linux ( Debian Surge )

Thanks

J

Roman Yakovenko wrote:

Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.

Roman



--
*John Abel
Senior Unix Administrator*
PA News Limited
www.pa.press.net <http://www.pa.press.net>
E-Mail address: jo*******@pa.press.net <mailto:jo*******@pa.press.net>
Telephone Number : 01430 455553
Fax Number : 0870 1240192
Mobile Number : 07971 611356
The Bishop's Manor, Market Place, Howden, DN14 7BL
PA News Limited, 292 Vauxhall Bridge Road, London SW1V 1AE. Registered
in England No. 3891053.

Jul 18 '05 #9

P: n/a
Roman Yakovenko wrote:
Hi. I have small problem. I need to load extension module that depends on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.


Looks like it's glibc linker inflexibility:
http://hathawaymix.org/Weblog/2004-12-30
"""
There is no provision for modifying the library search path once your
program has started.
"""

Python does update enviromental variables if you change os.environ or
call os.putenv, but the linker ignores the changes.

Serge.

Jul 18 '05 #10

P: n/a
On 31 Mar 2005 00:51:21 -0800, Serge Orlov <Se*********@gmail.com> wrote:
Roman Yakovenko wrote:
Hi. I have small problem. I need to load extension module that depends
on shared library. Before actually importing module I tried to edit
os.environ or to call directly to os.putenv without any success -
shared library was not found. I tried to search the Internet for the
answer. The only approach I saw was to set LD_LIBRARY_PATH before
invoking python script. I don't like this solution.


Looks like it's glibc linker inflexibility:
http://hathawaymix.org/Weblog/2004-12-30
"""
There is no provision for modifying the library search path once your
program has started.
"""


Thanks, well small script around my program will make the trick. I
think I have no other choise.
Python does update enviromental variables if you change os.environ or
call os.putenv, but the linker ignores the changes.
Serge.

--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #11

P: n/a
Roman Yakovenko wrote:
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

On Mar 31, 2005 9:35 AM, John Abel <jo*******@pa.press.net> wrote:
With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.

I think I should have permissions to do it. (more over users of my
scripts should have permissions )


Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal
Jul 18 '05 #12

P: n/a
Joal Heagney wrote:
Roman Yakovenko wrote:
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

On Mar 31, 2005 9:35 AM, John Abel <jo*******@pa.press.net> wrote:
With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.


I think I should have permissions to do it. (more over users of my
scripts should have permissions )

Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal


Aaaarrrrggghhhh. Too long since I've programmed in script. Plus it
doesn't help changing your mind about implementation halfway through.
The script should read something like this:

-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library"
then
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

Sorry about that.

pretending-like-a-cat-that-he-didn't-just-do-something-stupid'ly yours,

Joal
Jul 18 '05 #13

P: n/a
Op 2005-03-31, Joal Heagney schreef <jo**@bigpond.net.au>:
Joal Heagney wrote:
Roman Yakovenko wrote:
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

On Mar 31, 2005 9:35 AM, John Abel <jo*******@pa.press.net> wrote:

With Solaris 8+ you would use the command crle, with Linux
(RedHat/SuSE/Mandrake) you need to add the relevant directories
/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
I can't say if it matches, but that should give you a pointer.

I think I should have permissions to do it. (more over users of my
scripts should have permissions )

Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal


Aaaarrrrggghhhh. Too long since I've programmed in script. Plus it
doesn't help changing your mind about implementation halfway through.
The script should read something like this:

-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library"
then
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------


And you should change that last line to:

<wrapped program> "$@"
--
Antoon Pardon
Jul 18 '05 #14

P: n/a
Antoon Pardon wrote:
Op 2005-03-31, Joal Heagney schreef <jo**@bigpond.net.au>:
Joal Heagney wrote:
Roman Yakovenko wrote:
Thanks for help. But it is not exactly solution I am looking for. I
would like to do it from python script. For example

update_env() #<- this function will change LD_LIBRARY_PATH
import extension_that_depends_on_shared_library

Roman

On Mar 31, 2005 9:35 AM, John Abel <jo*******@pa.press.net> wrote:
>With Solaris 8+ you would use the command crle, with Linux
>(RedHat/SuSE/Mandrake) you need to add the relevant directories
>/etc/ld.so.conf and run ldconfig. I've not got a Debian box to hand, so
>I can't say if it matches, but that should give you a pointer.

I think I should have permissions to do it. (more over users of my
scripts should have permissions )
Yep. Unfortunatly if you don't have access to the /etc/ld.so.conf file,
the only option left is your wrapper script idea. (By the way, have you
actually tested to see if setting the LD_LIBRARY_PATH actually works? If
not, you're really up the creek.)

If the script is in shell, you could use something like:

(Dotted lines denote start and end of script, not actual script content)
-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library" then
export LD_LIBRARY_PATH=$oldpath":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

This will check to see if your library path is in the LD_LIBRARY_PATH,
set it if it's not, and then run your wrapped program, passing it the
arguments that the wrapper script was called by.

Joal


Aaaarrrrggghhhh. Too long since I've programmed in script. Plus it
doesn't help changing your mind about implementation halfway through.
The script should read something like this:

-----------------------------------------------------------
#!/bin/sh
if ! echo ${LD_LIBRARY_PATH} | /bin/fgrep -q "/path/to/your/library"
then
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH":/path/to/your/library"
fi
<wrapped program> $*
-----------------------------------------------------------

And you should change that last line to:

<wrapped program> "$@"


Ah yes, because we want the arguments passed in as seperate words, not
as a whole string.

Serves me right for testing this out as:

#!/bin/sh
echo $*

:)

Joal
Jul 18 '05 #15

P: n/a
On Fri, 01 Apr 2005 01:13:03 GMT, rumours say that Joal Heagney
<jo**@bigpond.net.au> might have written:
<wrapped program> $*
-----------------------------------------------------------

And you should change that last line to:

<wrapped program> "$@"


Ah yes, because we want the arguments passed in as seperate words, not
as a whole string.


No, this would happen if your last line was

<wrapped program> "$*"
To summarize, suppose your script is called with the following
arguments:

<wrapper_script> "File with space.txt" arg2 arg3

Here follow "last lines" and the corresponding sys.argv[1:]:

LAST LINE: <wrapped_program> $*
SYS.ARGV : ["File", "with", "space.txt", "arg2", "arg3"]

LAST LINE: <wrapped_program> "$*"
SYS.ARGV : ["File with space.txt arg2 arg3"]

LAST LINE: <wrapped_program> "$@"
SYS.ARGV : ["File with space.txt", "arg2", "arg3"]

For more information, see man 1 bash or man 1 ksh or man 1 sh. Don't
know if this applies to *csh family.
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.