Python program to print positive numbers in a list


In this article, the task is to print the positive numbers from a list of integers. Here, in this Python article, this task is done using the different methods in 4 different examples. Then these positive numbers are printed. In example 1, the positive numbers are picked and separated into another list. In the example 2, all the elements that are not positive are removed. In example 3, the sorted list is split up to zero and only positives are retained. In example 4, filter is used to select the positive numbers.

Example 1 - Select only the positive numbers in a list and separate these to another list to print.

Algorithm

Step 1 − Specify the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of integers between the given range

Step 2 − First separate all positive numbers in a separate list. Print these numbers.

Step 3 − Run the program and then check the result.

The Python File Contains this

lowNum=-100
highNum=100
mainlist=[]
listPos=[]

#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
   mainlist.append(item)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)

#dividing the main list into negatives and positives 
for item in mainlist:
   if (item > 0): 
      listPos.append(item)
            
print("\nThe Positive Elements in the List :")
print(listPos)

Viewing The Result - Example 1

For seeing the result run the Python file in the cmd window.

In the given range from  -100  to 100  :

The Main List :
[-100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

The Positive Elements in the List :
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

Fig 1: Showing the result in the command window.

Example 2: Remove all the non positive elements and print the remaining positive numbers.

Algorithm

Step 1 − Specify the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of randomly picked 100 integers between the given range

Step 2 − First sort this list and make a separate sorted list and then remove all less than zero numbers from this.

Step 3 − Print all the left-out positive numbers in the sorted list.

Step 4 − Run the program and then check the result.

The Python File Contains this

lowNum=-1000
highNum=1000
mainlist=[]
listPos=[]
#Making the 100 random elements list with integers starting from lowNum and upto HighNum
import random
#Generate 100 random numbers between lowNum and highNum
randomlist = random.sample(range(lowNum, highNum), 100)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe 100 element Random List :")
print(randomlist)

print("\nThe 100 element from Random List - Sorted:")
randomListSorted= sorted(randomlist)
print(randomListSorted)

#dividing the main list into negatives and positives 
for item in randomlist:
   if (item <= 0): 
      randomListSorted.remove(item)
            
print("\nThe Positive Elements in the List :")
print(randomListSorted)

Viewing The Result - Example 2

Open the cmd window and run the python file to see the result.

In the given range from  -1000  to 1000  :

The 100 element Random List :
[169, -420, 938, 508, 19, -232, -83, -118, 85, 639, -316, 940, 500, -708, 201, -301, 350, -350, -893, 690, 884, 412, 179, -768, -670, 737, -102, -760, -788, -968, -41, 591, -392, 551, 470, -912, -328, -697, -47, -229, -881, -69, 269, -771, 429, 686, -33, -844, 667, 962, 397, 110, -489, -550, -877, -136, -979, 955, -831, 151, -999, -584, -855, -667, -173, -883, -116, 521, 455, -237, 882, 82, 706, -500, 574, -992, 194, 778, 156, 974, 13, -401, -434, 891, 354, 863, 6, 417, 271, 599, 394, 457, -396, 552, 234, -904, 894, 221, 50, 627]

The 100 element from Random List - Sorted:
[-999, -992, -979, -968, -912, -904, -893, -883, -881, -877, -855, -844, -831, -788, -771, -768, -760, -708, -697, -670, -667, -584, -550, -500, -489, -434, -420, -401, -396, -392, -350, -328, -316, -301, -237, -232, -229, -173, -136, -118, -116, -102, -83, -69, -47, -41, -33, 6, 13, 19, 50, 82, 85, 110, 151, 156, 169, 179, 194, 201, 221, 234, 269, 271, 350, 354, 394, 397, 412, 417, 429, 455, 457, 470, 500, 508, 521, 551, 552, 574, 591, 599, 627, 639, 667, 686, 690, 706, 737, 778, 863, 882, 884, 891, 894, 938, 940, 955, 962, 974]

The Positive Elements in the List :
[6, 13, 19, 50, 82, 85, 110, 151, 156, 169, 179, 194, 201, 221, 234, 269, 271, 350, 354, 394, 397, 412, 417, 429, 455, 457, 470, 500, 508, 521, 551, 552, 574, 591, 599, 627, 639, 667, 686, 690, 706, 737, 778, 863, 882, 884, 891, 894, 938, 940, 955, 962, 974]

Fig 2: Showing the positive numbers as final result.

Example 3 - Split the sorted list upto zero and retain the positives to print.

Algorithm

Step 1 − Specify the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of randomly picked 100 integers between the given range

Step 2 − First sort this list and create a separate sorted list and then split this list up to zero.

Step 3 − Display all the left-out positive numbers presented in the sorted list.

Step 4 − Execute the program and then check the result.

The Python File Contains this

lowNum=-1000
highNum=1000
mainlist=[]
listPos=[]
#Making the 100 random elements list with integers starting from lowNum and upto HighNum
import random
#Generate 100 random numbers between lowNum and highNum
randomlist = random.sample(range(lowNum, highNum), 100)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe 100 element Random List :")
print(randomlist)

randomListSorted= sorted(randomlist)
print(randomListSorted)
listPos[:] = [item for item in randomListSorted if item >= 0]
          
print("\nThe Positive Elements in the List :")
print(listPos)

Viewing The Result - Example 3

For seeing the result run the Python file in the cmd window.

In the given range from  -1000  to 1000  :

The 100 element Random List :
[-263, 161, -346, -18, 187, 42, -385, 403, 609, -281, -787, -290, -819, -512, -953, -585, 850, -160, -380, -308, 959, 706, -434, -627, -31, -417, 358, -323, -333, -625, -468, 634, 283, -137, 692, -546, -747, -44, -749, -671, -8, 8, 628, 366, 710, -131, -438, -471, 347, 312, 491, -545, 299, -315, -41, -952, 843, 389, 180, -85, 738, 78, 569, -653, 978, 902, -875, -608, 782, -681, 848, -393, -339, -275, 514, 594, 368, -480, 435, 904, -693, 26, 543, -648, 477, 485, 880, -156, -966, 812, -933, -972, -96, -495, 503, 335, 276, -220, -149, -132]

The Positive Elements in the List :
[8, 26, 42, 78, 161, 180, 187, 276, 283, 299, 312, 335, 347, 358, 366, 368, 389, 403, 435, 477, 485, 491, 503, 514, 543, 569, 594, 609, 628, 634, 692, 706, 710, 738, 782, 812, 843, 848, 850, 880, 902, 904, 959, 978]

Fig 3: Showing the result in the command window.

Example 4: Using filter select the positive numbers and filter out the non positive ones.

Algorithm

Step 1 − Set the smallest and highest number in the range. The smallest number should be negative and the highest number should be positive. Make a list of randomly picked 100 integers between the given range

Step 2 − Use a filter with a lambda function.

Step 3 − Filter out the positives in a separate list. Print all the filtered positive numbers in the sorted list.

Step 4 − Run the program and then check the result.

The Python File Contains this

lowNum=-1000
highNum=1000
mainlist=[]
listPos=[]
#Making the 100 random elements list with integers starting from lowNum and upto HighNum
import random
#Generate 100 random numbers between lowNum and highNum
randomlist = random.sample(range(lowNum, highNum), 100)

print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe 100 element Random List :")
print(randomlist)

listPos = list(filter(lambda item: item >= 0, randomlist))
          
print("\nThe Positive Elements in the List :")
print(listPos)

Viewing The Result - Example 4

Open the cmd window and run the python file to see the result.

In the given range from  -1000  to 1000  :

The 100 element Random List :
[556, -122, 228, -157, -340, 636, 408, 143, 957, -835, -688, -163, -786, -525, 518, -767, 877, 637, -60, -733, -379, 4, 348, -378, 671, -980, 119, -350, -167, -124, 593, 564, -641, 413, -3, -152, -916, 600, -132, 426, -553, -768, 561, 282, 952, -252, -209, 375, -674, 254, 11, 567, -679, -683, 28, 961, 211, 797, 848, -220, 724, -270, 471, -15, 835, -593, -840, 407, -543, 113, -23, -979, -541, -828, -534, -384, 730, -529, -506, -721, -905, -331, -429, -518, -831, 605, 298, 690, -436, 862, -140, 693, -719, -368, 161, 937, 79, -817, 204, -227]

The Positive Elements in the List :
[556, 228, 636, 408, 143, 957, 518, 877, 637, 4, 348, 671, 119, 593, 564, 413, 600, 426, 561, 282, 952, 375, 254, 11, 567, 28, 961, 211, 797, 848, 724, 471, 835, 407, 113, 730, 605, 298, 690, 862, 693, 161, 937, 79, 204]

Fig 4: Showing the positive numbers as final result.

In this Python article, using four different examples, the ways to show how to find the positive numbers from a given list and to print these were given. First, in example 1, the positive numbers were separated into another list. In example 2, all the elements that were nonpositives were removed. In example 3, the sorted list was split and only positives were retained. In example 4, the filter was used to select the positive numbers.

Updated on: 10-Jul-2023

273 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements