rogerthat
rogerthat

Reputation: 23

print second lowest number from a list

I'm new to python and I have been forcing myself into logic first to resolve problems.

I have been trying to figure out the best way to compare float numbers within the list to get the lowest second numbers from the list.

numbers = [40, 40, 10, 10, 21, 21, 100,100, 400,400, 15, 15]

from the above list, I want to get: 15 and 15 since they are both the lowest numbers. I don't care if they duplicates, so no need to remove the duplicates with set()

My approach:

I thought about a for loop and compare it but I got stuck. Maybe enumerate can help me to compare the numbers?

Additionally, how do you recommend improving logic to resolve problems?I have been focusing on hackerrank exercises so far.

thank you!

Upvotes: 1

Views: 2642

Answers (5)

Gustavo Kawamoto
Gustavo Kawamoto

Reputation: 3067

You can do that with

second_smallest = sorted(set(numbers))[1]
[i for i in numbers if i == second_smallest]

Explaining:

  • set(numbers) will remove duplicates
  • sorted will order the list asc
  • Then we just loop over them and filter only the second smallest

HackerRank is a good place for you to improve, but you should also get involved in other projects that have wider tests and challenges. Stack Overflow is a good place for that :)

Upvotes: 2

pho
pho

Reputation: 25489

Since you're a beginner and you probably want to learn how to think about these problems in addition to the inbuilt way to do this, here's a method that doesn't use a prewritten function to sort the list. We're going to loop over the list and keep track of the smallest and second-smallest items.

smallestnum = [numbers[0]] # This will keep track of all smallest numbers we've found
secsmallest = [numbers[0]] # This will hold all second-smallest numbers we find
for element in numbers:
    if element < smallestnum[0]:
        # This is the new minimum
        # So the old minimum is now the second-smallest
        secsmallest = smallestnum
        smallestnum = [element]
    elif element == smallestnum[0]:
        # Add this number to our list of smallests
        smallestnum.append(element)
    elif element < secsmallest[0]:
        # This is the new second-smallest
        secsmallest = [element]
    elif element == secsmallest[0]:
        # Add this to our list of 2nd smallest
        secsmallest.append(element)
    # else do nothing

Upvotes: 2

Kelly Bundy
Kelly Bundy

Reputation: 27588

A working and efficient solution:

>>> s = set(numbers)
>>> s.remove(min(s))
>>> x = min(s)
>>> [x] * numbers.count(x)
[15, 15]

Or with O(1) space:

>>> x = min(numbers)
>>> y = min(y for y in numbers if y != x)
>>> [y] * numbers.count(y)
[15, 15]

Upvotes: 0

arya2004
arya2004

Reputation: 1

numbers = [40, 40, 10, 10, 21, 21, 100,100, 400,400, 15, 15]
def second_lowest_number():
        numbers.sort()
        z = numbers[0]
for i in numbers:
        if i > z:
            print(i)
            break
second_lowest_number()

This will give answer regardless of duplicates

Upvotes: 0

Jean-Marc Volle
Jean-Marc Volle

Reputation: 3323

You can do the following:

sorted(set(numbers))[1]
  • set(numbers) creates a set from the list: each value is kept only one
  • sorted(set(numbers)) sorts the values from smallest to tallest
  • sorted(set(numbers))[1] retrieves the second lowest

Upvotes: -1

Related Questions