return the last item in a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Bear

    return the last item in a list


    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.

  • Christos TZOTZIOY Georgiou

    #2
    Re: return the last item in a list

    On 30 Mar 2005 10:48:17 -0700, rumours say that David Bear
    <David.Bear@asu .edu> might have written:
    [color=blue]
    >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.[/color]

    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...

    Comment

    • Raymond Hettinger

      #3
      Re: return the last item in a list

      [David Bear][color=blue]
      > I'm looking for an 'easy' way to have the last item in a list returned.[/color]

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


      Raymond Hettinger


      Comment

      • Erik Max Francis

        #4
        Re: return the last item in a list

        Raymond Hettinger wrote:
        [color=blue][color=green]
        >> I'm looking for an 'easy' way to have the last item in a list returned.[/color]
        >
        > Try mylist.pop() or mylist[-1].[/color]

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

        --
        Erik Max Francis && max@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

        Comment

        • Roman Yakovenko

          #5
          LD_LIBRARY_PATH - how to set?

          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

          Comment

          • John Abel

            #6
            Re: LD_LIBRARY_PATH - how to set?

            What OS? Linux? Solaris?

            J

            Roman Yakovenko wrote:
            [color=blue]
            >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
            >
            >[/color]

            Comment

            • Roman Yakovenko

              #7
              Re: LD_LIBRARY_PATH - how to set?

              On Mar 31, 2005 9:20 AM, John Abel <john.abel@pa.p ress.net> wrote:[color=blue]
              > What OS? Linux? Solaris?[/color]

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

              Thanks

              [color=blue]
              > J
              >
              > Roman Yakovenko wrote:
              >[color=green]
              > >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
              > >
              > >[/color]
              >[/color]

              Comment

              • John Abel

                #8
                Re: LD_LIBRARY_PATH - how to set?

                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:
                [color=blue]
                >On Mar 31, 2005 9:20 AM, John Abel <john.abel@pa.p ress.net> wrote:
                >
                >[color=green]
                >>What OS? Linux? Solaris?
                >>
                >>[/color]
                >
                >Does it matter? If so, please explain why ( lack of knowledge )
                >I am using Linux ( Debian Surge )
                >
                >Thanks
                >
                >
                >
                >[color=green]
                >>J
                >>
                >>Roman Yakovenko wrote:
                >>
                >>
                >>[color=darkred]
                >>>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
                >>>
                >>>
                >>>
                >>>[/color][/color]
                >
                >
                >[/color]

                --
                *John Abel
                Senior Unix Administrator*
                PA News Limited
                www.pa.press.net <http://www.pa.press.ne t>
                E-Mail address: john.abel@pa.pr ess.net <mailto:john.ab el@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.

                Comment

                • Roman Yakovenko

                  #9
                  Re: LD_LIBRARY_PATH - how to set?

                  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_shar ed_library

                  Roman

                  On Mar 31, 2005 9:35 AM, John Abel <john.abel@pa.p ress.net> wrote:[color=blue]
                  > 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.[/color]

                  I think I should have permissions to do it. (more over users of my
                  scripts should have permissions )
                  [color=blue]
                  > HTH
                  >
                  > J
                  >
                  > Roman Yakovenko wrote:
                  >[color=green]
                  > >On Mar 31, 2005 9:20 AM, John Abel <john.abel@pa.p ress.net> wrote:
                  > >
                  > >[color=darkred]
                  > >>What OS? Linux? Solaris?
                  > >>
                  > >>[/color]
                  > >
                  > >Does it matter? If so, please explain why ( lack of knowledge )
                  > >I am using Linux ( Debian Surge )
                  > >
                  > >Thanks
                  > >
                  > >
                  > >
                  > >[color=darkred]
                  > >>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
                  > >>>
                  > >>>
                  > >>>
                  > >>>[/color]
                  > >
                  > >
                  > >[/color]
                  >
                  > --
                  > *John Abel
                  > Senior Unix Administrator*
                  > PA News Limited
                  > www.pa.press.net <http://www.pa.press.ne t>
                  > E-Mail address: john.abel@pa.pr ess.net <mailto:john.ab el@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.
                  >[/color]

                  Comment

                  • Serge Orlov

                    #10
                    Re: LD_LIBRARY_PATH - how to set?

                    Roman Yakovenko wrote:[color=blue]
                    > Hi. I have small problem. I need to load extension module that[/color]
                    depends[color=blue]
                    > 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.[/color]

                    Looks like it's glibc linker inflexibility:

                    """
                    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.

                    Comment

                    • Roman Yakovenko

                      #11
                      Re: LD_LIBRARY_PATH - how to set?

                      On 31 Mar 2005 00:51:21 -0800, Serge Orlov <Serge.Orlov@gm ail.com> wrote:[color=blue]
                      > Roman Yakovenko wrote:[color=green]
                      > > Hi. I have small problem. I need to load extension module that[/color]
                      > depends[color=green]
                      > > 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.[/color]
                      >
                      > 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.
                      > """[/color]

                      Thanks, well small script around my program will make the trick. I
                      think I have no other choise.
                      [color=blue]
                      > 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
                      >[/color]

                      Comment

                      • Joal Heagney

                        #12
                        Re: LD_LIBRARY_PATH - how to set?

                        Roman Yakovenko wrote:[color=blue]
                        > 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_shar ed_library
                        >
                        > Roman
                        >
                        > On Mar 31, 2005 9:35 AM, John Abel <john.abel@pa.p ress.net> wrote:
                        >[color=green]
                        >>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.[/color]
                        >
                        >
                        > I think I should have permissions to do it. (more over users of my
                        > scripts should have permissions )[/color]

                        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_PA TH} | /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

                        Comment

                        • Joal Heagney

                          #13
                          Re: LD_LIBRARY_PATH - how to set?

                          Joal Heagney wrote:[color=blue]
                          > Roman Yakovenko wrote:
                          >[color=green]
                          >> 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_shar ed_library
                          >>
                          >> Roman
                          >>
                          >> On Mar 31, 2005 9:35 AM, John Abel <john.abel@pa.p ress.net> wrote:
                          >>[color=darkred]
                          >>> 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.[/color]
                          >>
                          >>
                          >>
                          >> I think I should have permissions to do it. (more over users of my
                          >> scripts should have permissions )[/color]
                          >
                          >
                          > 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_PA TH} | /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[/color]

                          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_PA TH} | /bin/fgrep -q "/path/to/your/library"
                          then
                          export LD_LIBRARY_PATH =$LD_LIBRARY_PA TH":/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

                          Comment

                          • Antoon Pardon

                            #14
                            Re: LD_LIBRARY_PATH - how to set?

                            Op 2005-03-31, Joal Heagney schreef <joal@bigpond.n et.au>:[color=blue]
                            > Joal Heagney wrote:[color=green]
                            >> Roman Yakovenko wrote:
                            >>[color=darkred]
                            >>> 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_shar ed_library
                            >>>
                            >>> Roman
                            >>>
                            >>> On Mar 31, 2005 9:35 AM, John Abel <john.abel@pa.p ress.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 )[/color]
                            >>
                            >>
                            >> 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_PA TH} | /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[/color]
                            >
                            > 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_PA TH} | /bin/fgrep -q "/path/to/your/library"
                            > then
                            > export LD_LIBRARY_PATH =$LD_LIBRARY_PA TH":/path/to/your/library"
                            > fi
                            ><wrapped program> $*
                            > -----------------------------------------------------------[/color]

                            And you should change that last line to:

                            <wrapped program> "$@"


                            --
                            Antoon Pardon

                            Comment

                            • Joal Heagney

                              #15
                              Re: LD_LIBRARY_PATH - how to set?

                              Antoon Pardon wrote:[color=blue]
                              > Op 2005-03-31, Joal Heagney schreef <joal@bigpond.n et.au>:
                              >[color=green]
                              >>Joal Heagney wrote:
                              >>[color=darkred]
                              >>>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_shar ed_library
                              >>>>
                              >>>>Roman
                              >>>>
                              >>>>On Mar 31, 2005 9:35 AM, John Abel <john.abel@pa.p ress.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_PA TH} | /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[/color]
                              >>
                              >>Aaaarrrrggghh hh. 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_PA TH} | /bin/fgrep -q "/path/to/your/library"
                              >>then
                              >> export LD_LIBRARY_PATH =$LD_LIBRARY_PA TH":/path/to/your/library"
                              >>fi
                              >><wrapped program> $*
                              >>-----------------------------------------------------------[/color]
                              >
                              >
                              > And you should change that last line to:
                              >
                              > <wrapped program> "$@"
                              >[/color]

                              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

                              Comment

                              Working...