Note that there are some explanatory texts on larger screens.

plurals
  1. POprinting every 2 elements in a tuple
    primarykey
    data
    text
    <p>I have looked at several questions <a href="https://stackoverflow.com/questions/5389507/iterating-over-every-two-elements-in-a-list">Iterating over every two elements in a list</a> and <a href="https://stackoverflow.com/questions/2631189/python-every-other-element-idiom?lq=1">Python &quot;Every Other Element&quot; Idiom</a>, they have yielded an answer that I might be forced to use but which I feel there is a better approach to; what I want to do with the code below is to be able to print every two or every 4 elements</p> <pre><code> Gun_rack =( 'Gun 1:', 'Colt New Frontier 44 Special', 'Cal:.44 ', 'Weight: 2.8 lbs', 'Ammo Capacity :6', 'Gun 2:', 'Smith &amp; Wesson SW1911DK' , 'Cal:.45 ACP ', 'Weight: 2.6 lbs', 'Ammo Capacity :9', 'Gun 3:', 'Heckler &amp; Koch P2000SK V2', 'Cal.357 ', 'Weight: 1.50 lbs', 'Ammo Capacity :13', 'Gun 4:', 'Magnum Research Desert Eagle', 'Cal.50 ', 'Weight: 4.46 lbs', 'Ammo Capacity :7', 'Gun 5:', 'Heckler &amp; Koch MP5K ', 'Cal 9mm ', 'Weight: 4.78 lbs', 'Ammo Capacity :30', ) </code></pre> <p>I know that if I used a dictionary I could print every key and value argument like the example given in the python tutorial, but the items are in random order hence the tuple</p> <pre><code>&gt;&gt;&gt; knights = {'gallahad': 'the pure', 'robin': 'the brave'} &gt;&gt;&gt; for k, v in knights.iteritems(): ... print k, v ... gallahad the pure robin the brave </code></pre> <p>I was hoping to use similar code with either a list or a tuple:</p> <pre><code>for a ,b in Gun_rack: print a,b </code></pre> <p>or </p> <pre><code>for a,b,c,d, in Gun_rack: print a,b,c,d, </code></pre> <p>I think I should have mentioned what the output should have been, instead of stepping trough the tuple and printing every 2nd element or every 4th element, it should go through the tuple and print every 2 elements then the next 2 elements until the end of the tuple.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload