printing out elements in list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • micklee74@hotmail.com

    #1

    printing out elements in list

    hi

    i have a list with contents like this
    alist = ['>QWER' , 'askfhs', '>REWR' ,'sfsdf' , '>FGDG',
    'sdfsdgffdgfdg' ]

    how can i "convert" this list into a dictionary such that

    dictionary = { '>QWER':'askfhs ' , '>REWR' : 'sfsdf' , '>FGDG',
    'sdfsdgffdgfdg' }

    thanks

  • Tim N. van der Leeuw

    #2
    Re: printing out elements in list

    Using slices and built-in zip:
    [color=blue][color=green][color=darkred]
    >>> alist = ['>QWER' , 'askfhs', '>REWR' ,'sfsdf' , '>FGDG', 'sdfsdgffdgfdg' ]
    >>> dict(zip(alist[::2], alist[1::2]))[/color][/color][/color]
    {'>QWER': 'askfhs', '>FGDG': 'sdfsdgffdgfdg' , '>REWR': 'sfsdf'}

    Slightly more efficient might be to use izip from itertools:
    [color=blue][color=green][color=darkred]
    >>> from itertools import izip
    >>> dict(izip(alist[::2], alist[1::2]))[/color][/color][/color]
    {'>QWER': 'askfhs', '>FGDG': 'sdfsdgffdgfdg' , '>REWR': 'sfsdf'}

    And perhaps using islice from iterools might improve efficiency even
    more:
    [color=blue][color=green][color=darkred]
    >>> from itertools import islice, izip
    >>> dict(izip(islic e(alist, 0, None, 2), islice(alist, 1, None, 2)))[/color][/color][/color]
    {'>QWER': 'askfhs', '>FGDG': 'sdfsdgffdgfdg' , '>REWR': 'sfsdf'}


    (I didn't try to time any of these solutions so I have no real idea
    which is more efficient, but using iterators from the itertools-module
    should in theory mean you create less temporary objects; especially
    with large lists this can be a win)

    Cheers,

    --Tim

    Comment

    • I V

      #3
      Re: printing out elements in list

      On Mon, 08 May 2006 00:44:39 -0700, micklee74 wrote:[color=blue]
      > i have a list with contents like this
      > alist = ['>QWER' , 'askfhs', '>REWR' ,'sfsdf' , '>FGDG',
      > 'sdfsdgffdgfdg' ]
      >
      > how can i "convert" this list into a dictionary such that
      >
      > dictionary = { '>QWER':'askfhs ' , '>REWR' : 'sfsdf' , '>FGDG',
      > 'sdfsdgffdgfdg' }[/color]

      This strikes me as a little bit voodish, but you could do:

      dictionary = dict(zip(alist[::2], alist[1::2]))

      Comment

      • micklee74@hotmail.com

        #4
        Re: printing out elements in list

        thanks for all your replies...I will go test them out..
        I was wondering what does this mean alist[1::2]?
        thanks

        Comment

        • Tim N. van der Leeuw

          #5
          Re: printing out elements in list

          alist[::2] means taking a slice. You should look up slice-syntax in the
          tutorials and reference manual.

          in general,

          alist[1:5] means: take list elements position 1 up to (excluding) 5.
          (List indexing starts at position 0, so the first element in the list
          is not included!)
          alist[0:5] means: take list elements position 0 up to (excluding) 5; in
          other words: the first 5 elements.
          A shortcut for this is: alist[:5] -- omitting an index position means a
          default of 'start' resp. 'end'.
          So to take all elements from the 5th to the end of list, you write:
          alist[4:] (remember that indexing starts at position 0, so 0 is your
          first element, 4 is your 5th).

          To take a slice that is the whole list, write: alist[:]

          Slices discussed so far take all elements in the indicated range,
          however you can specify a 'step' with your slices. Default step is 1,
          but to skip every other element you write:
          alist[::2]
          Which takes all elements of your list, starting at position 0, adding 2
          to the index each step, so next is item 2, then 4, etc, until end of
          list.
          Now we have all the 'even-numbered' elements in the list, to get the
          'odd-numbered elements' write:
          alist[1::2]

          I hope this helps.


          Cheers,

          --Tim

          Comment

          • micklee74@hotmail.com

            #6
            Re: printing out elements in list

            thanks for the detailed explaination... i know about basic lists
            slicing..just havn't seen one with "steps" yet..
            thanks again...clp rocks.

            Comment

            Working...