434,695 Members | 1,338 Online
Need help? Post your question and get tips & solutions from a community of 434,695 IT Pros & Developers. It's quick & easy.

Loop in a loop?

 P: n/a Hi, I'm new to Python and have come across a problem I don't know how to solve, enter com.lang.python :) I'm writing some small apps to learn the language, and I like it a lot so far. My problem I've stumbled upon is that I don't know how to do what I want. I want to do a loop in a loop. I think. I've got two arrays with some random stuff in, like this. array1 = ['one','two','three','four'] array2 = ['a','b','c','d'] I want to loop through array1 and add elements from array2 at the end, so it looks like this: one a two b three c four c I'm stuck. I know how to loop through the arrays separatly and print them, but both at the same time? Hmmm. A push in the right direction, anyone? R, SH Jan 17 '08 #1
23 Replies

 P: n/a On Jan 17, 1:21 pm, Sacred Heart

 P: n/a On Jan 17, 1:35 pm, cokofree...@gmail.com wrote: for i in zip(array1, array2): print i Although I take it you meant four d, the issue with this method is that once you hit the end of one array the rest of the other one is ignored. Yes, small typo there. Okey, so if my array1 is has 4 elements, and array2 has 6, it won't loop trough the last 2 in array2? How do I make it do that? R, SH Jan 17 '08 #3

 P: n/a On Jan 17, 2:35 pm, cokofree...@gmail.com wrote: On Jan 17, 1:21 pm, Sacred Heart

 P: n/a On Jan 17, 2:52 pm, Chris

 P: n/a Sacred Heart a écrit : On Jan 17, 1:35 pm, cokofree...@gmail.com wrote: >for i in zip(array1, array2): print iAlthough I take it you meant four d, the issue with this method isthat once you hit the end of one array the rest of the other one isignored. Yes, small typo there. Okey, so if my array1 is has 4 elements, and array2 has 6, it won't loop trough the last 2 in array2? How do I make it do that? Please gentlemen: Python has no builtin type named 'array', so s/array/list/g Just pad your shortest list. Jan 17 '08 #6

 P: n/a > Yes, small typo there. Okey, so if my array1 is has 4 elements, and array2 has 6, it won't loop trough the last 2 in array2? How do I make it do that? Please gentlemen: Python has no builtin type named 'array', so s/array/list/g Just pad your shortest list. I agree, but was merely showing how he would use the variables he had given. Jan 17 '08 #7

 P: n/a Chris

 P: n/a On 17 Jan, 13:21, Sacred Heart

 P: n/a On 17 Jan, 14:38, Sacred Heart

 P: n/a On Jan 17, 4:38 pm, Bruno Desthuilliers

 P: n/a Sacred Heart schreef: On Jan 17, 1:35 pm, cokofree...@gmail.com wrote: >for i in zip(array1, array2): print iAlthough I take it you meant four d, the issue with this method isthat once you hit the end of one array the rest of the other one isignored. Yes, small typo there. Okey, so if my array1 is has 4 elements, and array2 has 6, it won't loop trough the last 2 in array2? How do I make it do that? One solution is with map() instead if zip(). map() with None as the first argument works much like zip(), but it keeps looping if one of the lists is exhausted. When that happens, it uses None for those values: words = ['zero', 'one', 'two', 'three'] numbers = [0, 1, 2, 3, 4, 5, 6] for word, number in map(None, words, numbers): print word, number zero 0 one 1 two 2 three 3 None 4 None 5 None 6 -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven Jan 17 '08 #12

 P: n/a Sacred Heart

 P: n/a On Jan 17, 12:25 pm, Paul Hankin

 P: n/a On Jan 17, 7:02*pm, George Sakkis

 P: n/a George Sakkis

 P: n/a Paul Rubin

 P: n/a On Jan 17, 7:13 pm, Paul Rubin

 P: n/a Ben Finney

 P: n/a On Jan 17, 11:59*pm, Paul Hankin

 P: n/a Roel Schroeven a Ã©crit : Sacred Heart schreef: >On Jan 17, 1:35 pm, cokofree...@gmail.com wrote: >>for i in zip(array1, array2): print iAlthough I take it you meant four d, the issue with this method isthat once you hit the end of one array the rest of the other one isignored. Yes, small typo there.Okey, so if my array1 is has 4 elements, and array2 has 6, it won'tloop trough the last 2 in array2? How do I make it do that? One solution is with map() instead if zip(). map() with None as the first argument works much like zip(), but it keeps looping if one of the lists is exhausted. When that happens, it uses None for those values: Yek ! Should have read the doc more carefully. Height years of Python, and I didn't knew this one :( Jan 18 '08 #21

 P: n/a Paul Rubin a écrit : George Sakkis And if the iterables don't necessarily support len(), here's a moregeneral solution: Not trying to pick on you personally but there's this disease when a newbie comes with a basically simple question (in this case, how to solve the problem with ordinary lists) and gets back a lot of complex, overly general "graduate level" solutions. As far as I'm concerned, it's certainly a GoodThing(tm) - everyone learns in the process. Jan 18 '08 #22

 P: n/a Hehe.. I remember seeing a similar one for Java and "Hello world" using more and more elaborate abstractions and design patterns but I can't find the link. George This is not linked to Java but deals with Hello World http://www.ariel.com.au/jokes/The_Ev...rogrammer.html Jan 18 '08 #23

 P: n/a On Jan 17, 7:39 pm, Paul Rubin