473,811 Members | 3,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

any such thing as list interleaving?


I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList ,range(len(aLis t)):
# ...

This definitely seems like a roundabout way to loop through
parallel lists together. Is this map routine truly the easiest/
best/most straight-forward way to do a for loop through parallel
lists, if I feel that the Python anti-idom of:

for index in range(len(myLis t)):
entry = aList(index)
anotherEntry = anotherList(ind ex)
# ...

???

This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)

Thanks.

-tom!
Jul 18 '05 #1
2 2643
Tom Plunket wrote:
I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList ,range(len(aLis t)):
# ...
In Python 2.3, you can write

for index, entry in enumerate(L):
# ...

For 2.2, you can define enumerate yourself:

def enumerate(L):
i = 0
while 1:
try:
yield i, L[i]
except IndexError:
return
i += 1

For older versions, yet another definition would be needed;
I leave that as an exercise.
This also brings up a similar problem for me when iterating over
dictionaries:

for key in myDict:
value = myDict[key]
# ...

This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)


Here, you could always write

for key, value in myDict.items():
#...

Since 2.2, there is another method available which does not create
a list of tuples:

for key, value in myDict.iteritem s():
#...

HTH,
Martin
Jul 18 '05 #2
On Sat, 12 Jul 2003 14:00:49 -0700, Tom Plunket <to***@fancy.or g>
wrote:

I find myself often doing the following sort of thing (sorry for
lack of whitespace, I don't want the line to break):

for entry, index in map(lambda e,i:(e,i),aList ,range(len(aLis t)):
# ...

This definitely seems like a roundabout way to loop through
parallel lists together. Is this map routine truly the easiest/
best/most straight-forward way to do a for loop through parallel
lists, if I feel that the Python anti-idom of:

Check the zip builtin:
help(zip) Help on built-in function zip:

zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th
element
from each of the argument sequences. The returned list is
truncated
in length to the length of the shortest argument sequence.

for index in range(len(myLis t)):
entry = aList(index)
anotherEntry = anotherList(ind ex)
# ...

???

This also brings up a similar problem for me when iterating over
dictionaries :

for key in myDict:
value = myDict[key]
# ...

Fire the interpreter and type:
help(dict)


As it's a long stretch of text, I'll just post the relevant part:

| iteritems(...)
| D.iteritems() -> an iterator over the (key, value) items of D
|
This seems a pretty sloppy way to go about it, imo. There must
be something more in the Python spirit! :)

Thanks.

-tom!


With my best regards,
G. Rodrigues
Jul 18 '05 #3

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

Similar topics

4
1509
by: Phil Schmidt | last post by:
The following example works fine (Python 2.3), but is it always safe to modify a list that is being iterated in a loop, regardless of the actual contents of the list x? If not, what's a better (safe) way to do it? Thanks! >>> x= >>> for t in x:
17
2132
by: Thomas M. | last post by:
Hello, i have a question on the following code. test_list = for i in test_list: print i
2
4641
by: Tim Marshall | last post by:
The following is happening in two instances in A2003. The one I s\describe is the easiest one. Theme controls and autocorrupt have been turned off long ago. An unbound main form with a list box (query row source and multi-select turned off) and a datasheet subform. The short cut menu for the datasheet subform includes standard sort ascending and descending and remove filter/sort menu items. The recordsource for the subform datasheet...
3
1373
by: Geoff | last post by:
Hi, all, I have a VB .NET (VS 2003) application that has a dropdown list or dropdown combo with checkboxes as a "3rd party control" linked to a ComponentOne TrueDBGrid cell button click event. When the user clicks on the grid cell, a list (either standard list or check combo) drops down with an appropriate list of options to select. All Ok so far..
1
1566
by: estebistec | last post by:
Hello, I've developed a CompositeControl in C# for ASP.NET 2.0. In this control I am simulating Edit and View modes by hiding or showing appropriate controls (setting Control.Visible). Basically I'm simulating the functionality of the DetailsView or a GridView cell, except that my data is dynamic (variable number of fields), so I can't just use those. So I have custom edit and view controls for this dynamic data and I render one of...
1
1230
by: Rich | last post by:
Hello, my project (vb2005) contains several classes that each produce lists of data which get stored/displayed in ado.net tables that have the same structure for each of the lists produced by each class. Class1 produces 3 lists which get stored in 3 ado.net tables (each table has a different structure). Class2 produces 3 similar lists that get stored in the same 3 ado.net tables. Class3...class8 also produce 3 lists per class that get...
17
1746
by: Me | last post by:
Hello, I have read all the reason why DOM purists don't like document.write and innerHTML. I would like to be a good programmer and do the right thing and not use them. But I need your help to get it to what the project needs done. In the interest of brevity, the following code has been simplified. You'll notice sDoc contains HTML and JavaScript, this is a unchangeable requirement. PLEASE don't waste your time or mine by
104
4624
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
5
1628
by: sudhivns | last post by:
Hi, I wann use the same collection in interleaving loops. like F1(..) { foreach(child prd in parent) F2(prd ) }
0
9731
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
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
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
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...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
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.