'Print all numbers less than the last number in a list in python [duplicate]
How can I print only the numbers that are less than the last number in the list?
I am trying to print all numbers less than the last number in a list using python. The list is based on user input. Example numbers for user input are:
5
40
50
160
300
75
100 (the last number)
I do not want to print the first or the last number. The first number lists how many numbers in the list to check. My code is only providing the current numbers in the list. I can't figure out how to only get the numbers that are less than the last number in the list. I do not want to use functions or an array. This needs to be for/while/else/if/range or something in that realm.
lst = [] #the list
n = int(input()) #user input
for i in range(-1, n):
ele = int(input())
lst.append(ele) # adding the element'
print(*lst, sep = "\n")
Solution 1:[1]
def list_check(list):
big_number = list[-1]
for I in list[1:]:
if I < big_number:
print(I)
I don't understand how you want the user input to play a part in this, but if you run a list through this it will check the last number against all numbers in the list not including the first number.
Solution 2:[2]
I might be inclined to sort the items, because then when you might hit the items that are larger, you could stop the checking. I'd probably sort the list into a new list and try to check on efficiency.
Solution 3:[3]
This code will print the numbers less and add to list
number1 = 100
number2 = 0
list1 = []
while True:
number2 += 1
list1.append(number2)
if number2 == number1:
list1.remove(number1)
break
print(list1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | fudgemasterultra |
| Solution 2 | p wilson |
| Solution 3 | coderchad123lol |
