1

When I run the following code, I seem to 'lose' values, yet I'm not sure why. Are there any other ways to print out two lists next to each other?

What I wish to do is to create a list of integers, from the users input, in a long. It should then separate them into positive and negative values, in ascending order.

The original code is:

import numpy as np
import random
print( "(This will create a list of integers, UserInput long. Then separate"     
 "\nthem into positive and negative values in ascending order.)")

userI = input("\nHow many integers would you like, between the values of -100 and 100?: ")

userI = int(userI)

Ints = []

for A in range(userI):
    Ints.append(random.randint(-100, 100))

print('\n', len(Ints))

def quad(N):
    Ints_pos = []
    Ints_neg = []
    for B in N:
        if B >= 0:
            Ints_pos.append(B)
        else:
            Ints_neg.append(B)

    return (Ints_pos, Ints_neg)

pos, neg = quad(N = Ints)

pos = sorted(pos)
neg = sorted(neg, reverse=True)

print('\nPositive', '             Negative'
  '\nValues:',  '              Values:')


for C, D in zip(pos, neg):
    print('-> ', C, '               -> ', D)

input("\nPress 'Enter' to exit")
SirJames
  • 387
  • 8
  • 27

1 Answers1

6

Keep in mind that when using zip:

The iterator stops when the shortest input iterable is exhausted.

You should consider using itertools.zip_longest and provide a dummy value to pad the shorter iterable.

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • Tried doing this and I don't add a dummy value because that defeats the purpose of the code. – Vincent Good Feb 17 '17 at 21:47
  • @VincentGood I'm not sure I understand what you mean by *defeats the purpose of the code.* – Moses Koledoye Feb 17 '17 at 21:53
  • The purpose of the code is the print two lists, one with negative values, the other with positive values. The amount of values in those lists is dictated by the user input. If I provide a dummy value I am adding to the amount of values that the user decided on. – Vincent Good Feb 17 '17 at 21:56
  • Sounds reasonable, but there isn't any way to print two lists with mismatching lengths side-by-side, without losing out from the longer list. The dummy value could be a letter, for example, so you'll know those are not part of the user's values – Moses Koledoye Feb 17 '17 at 21:59
  • Thanks, I'll do that. This was by far the best answer I've come across considering that you can't print two list with mismatched lengths side-by-side. – Vincent Good Feb 17 '17 at 22:05