Python shortcut ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Santanu Chatterjee

    Python shortcut ?

    Hello everybody,

    I am very new to Python. So pardon me if this question is
    too dumb.

    Suppose I have the following list:
    myList = [('a','hello), ('b','bye')]

    How do I get only the first element of each tuple in the
    above list to be printed without using:
    for i in range(len(myLis t)):
    print myList[i][0]
    or:
    for i in myList:
    print i[0]

    I tried:
    print myList[:][0]
    but it seems to have an altogether different meaning.

    Regards,
    Santanu

  • Santanu Chatterjee

    #2
    Re: Python shortcut ?

    Please ignore this message. It had been written much before
    my other thread on 'A Query about List' and for some reason
    had stayed in the 'send later' folder of Pan, and accidentally
    got posted today.

    Sorry.

    Regards,
    Santanu

    Comment

    • David Eppstein

      #3
      Re: Python shortcut ?

      In article <pan.2003.09.10 .19.44.10.27051 1@softhome.net> ,
      "Santanu Chatterjee" <santanu@softho me.net> wrote:
      [color=blue]
      > Suppose I have the following list:
      > myList = [('a','hello), ('b','bye')]
      >
      > How do I get only the first element of each tuple in the
      > above list to be printed without using:
      > for i in range(len(myLis t)):
      > print myList[i][0]
      > or:
      > for i in myList:
      > print i[0]
      >
      > I tried:
      > print myList[:][0]
      > but it seems to have an altogether different meaning.[/color]

      If you're set on a one-liner, you could try

      print zip(*myList)[1]

      or

      print [word for letter,word in myList]

      These are no quite the same -- zip makes tuples, the list comprehension
      makes a list. My own preference would be for the list comprehension --
      it's not as concise, but the meaning is clearer.

      --
      David Eppstein http://www.ics.uci.edu/~eppstein/
      Univ. of California, Irvine, School of Information & Computer Science

      Comment

      • Santanu Chatterjee

        #4
        Re: Python shortcut ?

        On Sun, 12 Oct 2003 13:21:21 -0700, David Eppstein wrote:
        [color=blue]
        > In article <pan.2003.09.10 .19.44.10.27051 1@softhome.net> ,
        > "Santanu Chatterjee" <santanu@softho me.net> wrote:
        >[color=green]
        >> Suppose I have the following list:
        >> myList = [('a','hello), ('b','bye')]
        >>
        >> How do I get only the first element of each tuple in the above list to
        >> be printed without using:
        >> for i in range(len(myLis t)):
        >> print myList[i][0][/color][/color]
        [color=blue][color=green]
        >> I tried:
        >> print myList[:][0]
        >> but it seems to have an altogether different meaning.[/color]
        >
        > If you're set on a one-liner, you could try
        > print zip(*myList)[1]
        > or
        > print [word for letter,word in myList]
        >
        > These are no quite the same -- zip makes tuples, the list comprehension
        > makes a list. My own preference would be for the list comprehension --
        > it's not as concise, but the meaning is clearer.[/color]

        Thanks for the solution. I was already using the second solution.
        I did not know about the zip function.

        Regards,
        Santanu

        Comment

        • Santanu Chatterjee

          #5
          Re: Python shortcut ?

          On Sun, 12 Oct 2003 13:21:21 -0700, David Eppstein wrote:
          [color=blue]
          > In article <pan.2003.09.10 .19.44.10.27051 1@softhome.net> ,
          > "Santanu Chatterjee" <santanu@softho me.net> wrote:
          >[color=green]
          >> Suppose I have the following list:
          >> myList = [('a','hello), ('b','bye')]
          >>
          >> How do I get only the first element of each tuple in the above list to
          >> be printed without using:
          >> for i in range(len(myLis t)):
          >> print myList[i][0]
          >> or:
          >> for i in myList:
          >> print i[0]
          >>
          >> I tried:
          >> print myList[:][0]
          >> but it seems to have an altogether different meaning.[/color]
          >
          > If you're set on a one-liner, you could try
          > print zip(*myList)[1]
          > or
          > print [word for letter,word in myList]
          >
          > These are no quite the same -- zip makes tuples, the list comprehension
          > makes a list. My own preference would be for the list comprehension --
          > it's not as concise, but the meaning is clearer.[/color]

          Thanks for the solution. I was already using the second solution.
          I did not know about the zip function.

          Regards,
          Santanu

          Comment

          Working...