473,767 Members | 1,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

May 8 '06 #1
5 1811
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:
from itertools import islice, izip
dict(izip(islic e(alist, 0, None, 2), islice(alist, 1, None, 2)))

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

May 8 '06 #2
I V
On Mon, 08 May 2006 00:44:39 -0700, micklee74 wrote:
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]))
May 8 '06 #3
thanks for all your replies...I will go test them out..
I was wondering what does this mean alist[1::2]?
thanks

May 8 '06 #4
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

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

May 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6385
by: sam | last post by:
Hi, I use "list" data container from STL. I m wondering how to print out all items from a list thru its iterator? I currently use two "for loop" for each list: list<HashMap>::iterator l_iter; HashMap::iterator m_iter; for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) { for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
5
2226
by: Mark Preston | last post by:
Admission first - I don't actually have a problem here but have noticed that a lot of people have been asking similar questions and getting very varied answers. What I've done is to sort of "compile the questions" into a theoretical problem to see what people think should be done to solve it. Maybe it will be a worthwhile discussion, but more importantly maybe it will find out the very best way to sort this kind of problem out so that...
5
2168
by: compboy | last post by:
How do you print elements of the list in one line? alist = so it will be like this: 1, 2, 5, 10, 15 because if I use this code for i in alist:
26
17958
by: unexpected | last post by:
If have a list from 1 to 100, what's the easiest, most elegant way to print them out, so that there are only n elements per line. So if n=5, the printed list would look like: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 etc.
8
17744
by: Steve Macleod | last post by:
Hi, I was wondering if anyone had a solution for printing HTML elements (especially style elements). I do not wish to make any changes to the page, other than in the <css media="print"block. I can use: select { border:1px solid #FFFFFF; } to get rid of the border, but dont know a way to get rid of the arrow
2
1587
by: Billy | last post by:
Hi, It has to be some stupid high school home task , that can be solved in rather straightforward manner. The pain in the @ss is the requirement not to touch the original list, that is: no swap between the elements is permitted. O(n^2) is a bunch of time: the list can be even bubble sorted, but I really fail to figure out how the hell I'm supposed to handle the sorting without building any additional structures and the requirement not...
8
5912
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
3
1365
by: William Chang | last post by:
Is the different behavior between __repr__ and __str__ intentional when it comes to printing lists? Basically I want to print out a list with elements of my own class, but when I overwrite __str__, __str__ doesn't get called but if I overwrite __repr__, __repr__ will get called. Is this a bug? For example:
0
1429
by: Deceneu | last post by:
Hi everyone, this is my first post so please bear with me. I have the following situation: i have a local report that needs to have two "versions": one for viewing in the report viewer (with all elements: lines, borders, "labels", etc..) and another one for printing - it needs only the information from the dataset. I need this because i have a lot of "official" documents (medication prescriptions, sick leave etc...) so the user must see the...
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8838
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5280
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3533
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.