# Python Challenge - Find Xth Number In Order

This community-built FAQ covers the “Find Xth Number In Order” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the Python challenge Find Xth Number In Order

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

``````def getX(x, nums):

nums.sort()

return(nums[x-1])

print(getX(2, [6, 3, -1, 5]))
``````

I get the following error:

“Tests failed to run due to an error: “list index out of range”. Check your code and try again.”

What triggers this error? 1 is in the range (0,1,2,3).

Your code doesn’t include a necessary component:

If the input `x` is greater than the length of the list, or `nums` is empty, return `0` .

When you click the `Test Code` button, your code is run with 10 test cases. Evidently some of those cases include a value for `x` that is out of range (greater than the length of the list).

1 Like

Thanks! I’ll try to read more carefully next time.

``````def getX(x, nums):
if x > len(nums) or not nums: return 0
nums.sort()

return(nums[x-1])

print(getX(2, [6, 3, -1, 5]))
``````
1 Like
def getX(x, nums): if x > len(nums) or not nums: return 0 nums.sort() return nums[(x-1)] print(getX(2, [6, 3, -1, 5]))

I coded a sorting algorithm instead of using `.sort()`
algorithm:

Copy the list, and Create a empty list that will hold the sorted values.
For the copy, find the minimum, remove it, and append it to the list for the sorted values; repeat.

my code
``````def sorted_copy(nums): # O(n^2), not in-place sorting
copy = nums.copy()
sorted = []
length = len(copy)
for n in range(length): # iterates n times
m = min(copy) #iterates between 0 and n times
copy.remove(m) #iterates between 0 and n times
sorted.append(m)
return sorted

def getX(x, nums):
sorted_nums = sorted(nums)
length = len(sorted_nums)
if (x > 0 and x <= length):
return sorted_nums[x - 1]
elif (x >= -length and x < 0):
return sorted_nums[x]
else:
return 0

print(getX(2, [6, 3, -1, 5]))
``````

I know there’s more efficient algorithms out there.
Has anyone coded a bubble sort or another sorting algorithm for this challenge?