Question

In Python: Problem 5] Write a function that accepts a list as argument and returns a...

In Python:

Problem 5] Write a function that accepts a list as argument and returns a list that contains:

(1) The positive elements of the taken list (2) The negative elements of the taken list (3) The odd elements of the taken list
(4) The even elements of the taken list

Homework Answers

Answer #1

Below is your code:

#function definition
def getLists(list):
  #declaration of lists
  postiveList = []
  negativeList = []
  evenList = []
  oddList = []
  #loop to fill the list with respective values
  for x in range(0, len(list)):
    if list[x] < 0:
      negativeList.append(list[x])
    else:
      postiveList.append(list[x])
    if list[x] % 2 == 0:
      evenList.append(list[x])
    else:
      oddList.append(list[x])
  #return all the lists
  return postiveList, negativeList, oddList, evenList

#crating a sample list
listOfNums = [3,5,6,-2,-5,-6,34,76,-9,1,2,5,-7,8,9,-3,6,8,3]
#calling method and getting the returned lists
postiveList, negativeList, oddList, evenList = getLists(listOfNums)
#printing the output
print("Positive List:",postiveList)
print("Negative List:",negativeList)
print("Odd List:",oddList)
print("Even List:",evenList)

Indentation and output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
. Write a function in python that accepts a sentence as the argument and converts each...
. Write a function in python that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of...
In Python, write a function that takes in an arbitrary list of numeric triples (tuples of size 3), and returns a new list containing the maximum elements from each
Create a function that returns a boolean value if all-elements are not odd or even. Using...
Create a function that returns a boolean value if all-elements are not odd or even. Using Dr. Racket and the How to Design a Function (HtDF ) recipe, create a function that will return the following check-expect: (check-expect (all-elements? even? (list 1 2 3) false) (check-expect (all-elements? even? (list 2 4 6) true) (check-expect (all-elements? odd? (list 1 3 5) true) Can you find an element in the list where the predicate fails (return false)?
Create a function which returns the number of odd integers in a list (python)
Create a function which returns the number of odd integers in a list (python)
In Python: Sublist of list A is defined as a list whose elements are all from...
In Python: Sublist of list A is defined as a list whose elements are all from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6], its has many sublists and one of them is [0, 1, 3] because elements 0, 1 and 3 are all contained in list A. Define a function named returnComplement that accepts two integer lists as the parameter (one of the list is the sublist of the other). Suppose names...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT