printing out elements in list

M

micklee74

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
 
T

Tim N. van der Leeuw

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

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

And perhaps using islice from iterools might improve efficiency even
more:
{'>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
 
I

I V

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' }

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

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

micklee74

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

Tim N. van der Leeuw

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
 
M

micklee74

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,212
Latest member
SteveMagga

Latest Threads

Top