0

I am looking for a nice pythonian solution to read two elements out of a list in Python 3. What do I need to write for ??? in the following code:

it = [1,2,3,4,5,6]
for x, y in ??? :
    print (x, y)

The desired output would be:

1 2
3 4
5 6

Also one could solve it with indexed for-loop. But this is also ugly (IMHO)

Marcel Sonderegger
  • 772
  • 1
  • 8
  • 21

5 Answers5

4

Use zip(*[iter(it)] * 2), as seen in this answer.

it = [1,2,3,4,5,6]
for x, y in zip(*[iter(it)] * 2):
    print(x, y)
2

Another easy way without zip is:

i = 0
while i < len(it)-1:
  print(it[i], it[i+1])
  i += 2
M. Taylor
  • 21
  • 4
  • I agree it is easy, but it uses indexes: I try to avoid indexes whenever possible. But that's a matter of style – Marcel Sonderegger Jun 11 '18 at 13:37
  • 1
    Yes it uses indexes. I did not have read this, sorry. But maybe someone else will help this. Luckily there are enough answers which will help you. – M. Taylor Jun 11 '18 at 13:44
2

Another cheap option, using indices. You might not like them, but they are fast.

it = [1,2,3,4,5,6]
for x in range(0, len(it)-1, 2):
    print(it[x], it[x+1])
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
1

Here is another approach which will work in Python3:

def pair(a):
   # Or simply:
   # return zip(a[::2], a[1::2])
   for k, v in zip(a[::2], a[1::2]):
       yield k, v

a = [1,2,3,4,5,6]
final = list(pair(a))
print(final)

Output:

[(1, 2), (3, 4), (5, 6)]
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43
  • 1
    Ansering exactly the question the answer for the question marks would be: `zip(a[::2], a[1::2])` . In my opinion a very readable answer. Congratulation. – Marcel Sonderegger Jun 11 '18 at 14:43
0

Got this to work for your case:

l = [1,2,3,4,5,6]
print(*[' '.join([str(a) for a in x]) for x in zip(l[::2],l[1:][::2])],sep='\n')

First I had to use l[::2] to create a list the odd numbers in the list, then l[1:][::2] to get even numbers. Did this using slicing function which is quite useful link

Then I zipped them together to match 1st, 2nd, 3rd elements etc link

Then I used list comprehension to create a list of each of those sets. The problem is that they were sets and not text as above. To resolve this I changed my sets to string str() .

Now that the individual sets have been turned into string, you can join with ' '.

You still have a set of 3 but you can print each line with print(*[list here],sep='\n') link

Learned a few new tricks, hope this helps!

EoinS
  • 5,405
  • 1
  • 19
  • 32
  • 2
    That's unreadable. This won't pass any code review on earth. – Aran-Fey Jun 11 '18 at 13:51
  • 1
    @aran-fey I'm sure OP is worried about peer review as he submits this to the platform he is building. There are different approaches to solving problems on this site. I combined a few different functions and explain and note source for each. This site is educational and all solutions have merit – EoinS Jun 11 '18 at 14:03
  • 2
    Dear @EoinS, you made me smile when I read your code. I am sure it will work. I liked how you used a real unusual approach to solve the problem: going over strings. However, imagine yourself reunderstanding your own code in a few years. It could be difficult. – Marcel Sonderegger Jun 11 '18 at 14:31