index in "double" list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • BoÅ¡tjan Jerko

    index in "double" list

    Hello!

    I have a list of lists (e.g. [[1,"a"],[2,"b"],[3,"c"]]) and
    would like to do index on the first element of the lists.
    Can it be done?

    B.

  • Larry Bates

    #2
    Re: index in "double&qu ot; list

    If you have list:

    t=[[1,"a"],[2,"b"],[3,"c"]]

    The first element of each list contained
    in t is accessed by:

    t[n][0]

    where n is the element number you wish to access
    (zero based of course).

    -Larry

    "Boštjan Jerko" <bostjan.jerko@ mf.uni-lj.si> wrote in message
    news:mailman.11 54.1075795507.1 2720.python-list@python.org ...[color=blue]
    > Hello!
    >
    > I have a list of lists (e.g. [[1,"a"],[2,"b"],[3,"c"]]) and
    > would like to do index on the first element of the lists.
    > Can it be done?
    >
    > B.
    >[/color]


    Comment

    • Dan Bishop

      #3
      Re: index in &quot;double&qu ot; list

      Bo?tjan Jerko <bostjan.jerko@ mf.uni-lj.si> wrote in message news:<mailman.1 154.1075795507. 12720.python-list@python.org >...[color=blue]
      > Hello!
      >
      > I have a list of lists (e.g. [[1,"a"],[2,"b"],[3,"c"]]) and
      > would like to do index on the first element of the lists.
      > Can it be done?[/color]

      I think you might want to try using dicts.
      [color=blue][color=green][color=darkred]
      >>> d = {1: 'a', 2: 'b', 3: 'c'}
      >>> d[2][/color][/color][/color]
      'b'

      If you already have a list of lists, you can convert it to a dict with
      the constructor.
      [color=blue][color=green][color=darkred]
      >>> a = [[1, "a"], [2, "b"], [3, "c"]]
      >>> d = dict(a)[/color][/color][/color]

      Comment

      Working...