Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
217 views
in Computer by (51.9k points)
closed by

Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.

1 Answer

+1 vote
by (49.5k points)
selected by
 
Best answer
#Defining empty list

list1 = list()

#Getting the input of number of elements to be added in the list

inp = int(input("How many elements do you want to add in the list?

(Element can be both positive and negative) "))

#Taking the input of elements to be added

for i in range(inp):

a = int(input("Enter the elements: "))

list1.append(a)

#Printing the list

print("The list with all the elements: ",list1)

#Defining list2 and list3 to store positive and negative elements of the

list

list2 = list()

list3 = list()

#Looping through list to segregate positive and negative numbers

for j in range(inp):

if list1[j] < 0:

#Appending negative elements to list3

list3.append(list1[j])

else:

#Appending positive elements to list2

list2.append(list1[j])

print("The list with positive elements: ",list2)

print("The list with negative elements: ",list3)

OUTPUT:

How many elements do you want to add in the list? (Element can be

both positive and negative) 5

Enter the elements: -1

Enter the elements: -2

Enter the elements: -3

Enter the elements: 4

Enter the elements: 5

The list with all the elements: [-1, -2, -3, 4, 5]

The list with positive elements: [4, 5]

The list with negative elements: [-1, -2, -3]

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...