0

I used this code to make a list of float values ranging from 0 to 1:

whole_list = [random.random() for n in range(count)]

I want to group them into two lists big_list and small_list, so that big_list contains the values from whole_list that are greater than 0.5, and small_list contains the values less than 0.5

How can I do this in Python?

UPDATE: Solution is as follows:

    for n in whole_list:
        if n > cutoff:
            big_list.append(n)
        else: 
            small_list.append(n)
    print(big_list)
    print(small_list)
hanbone
  • 1
  • 1
  • 2
    `I have tried a few things and have gotten errors.`: Post those trials so that we can comment on them. Someone else writing the code for you won't teach you anything more than you can learn from reading any Python tutorial. – Selcuk Jan 30 '23 at 03:48
  • 1
    You could create two empty lists and then use a for loop and an if statement to append to each. – tdelaney Jan 30 '23 at 03:51
  • I have created two empty lists for big_list/small_list. how to i append them based on a condition? – hanbone Jan 30 '23 at 03:57
  • You can figure this out incrementally. Start with a for loop that just iterates the values of `whole_list` and prints them. Once that works, replace the print with an `if/else` that checks whether the values are < .5 - add prints to the if and else clauses to see them work. Once that works, replace those prints with list append calls. – tdelaney Jan 30 '23 at 04:03
  • *"based on whether they are < or > .5."* What about = .5? – Kelly Bundy Jan 30 '23 at 04:21
  • "how to i append them based on a condition?" Well, do you know how to check whether the condition is met? Do you know how to make certain code run `if` the condition is met, and different code otherwise (i.e. `else`, hint hint)? Do you know how to append to a list? If you put these things together, does it not solve the problem? Why not? – Karl Knechtel Jan 30 '23 at 04:35

0 Answers0