- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we are going to learn how to find the possible largest number from the given list of numbers. We will see two different ways to find to solve the problem. Follow the below steps to solve the problem.

- Import the itertools module for permutations method.
- Initialize the list with numbers and an empty list.
- Iterate over the permutations of the list.
- Join all the combinations and add the result to the empty list.

- Find the max number from the result with max method and key as int.
- Convert the string to integer and print it.

Let's see the code.

# importing the module import itertools # initializing the list numbers = [45, 35, 138, 43, 67] # result result = [] # permutations for permutation in itertools.permutations(str(number) for number in numbers): result.append(''.join(permutation)) # finding max maximum = max(result, key=int) # printing the max print(int(maximum))

If you run the above code, then you will get the following result.

67454335138

Let's see another way to solve the problem. We will sorted function to solve the problem. Follow the below steps to write the code.

- Pass the list to the sorted function.
- Write a function called get_key that accepts two arguments.
- Return -1 if the str(first) + str(second) > str(second) + str(first) else 1.
- Joining the list of elements using join method.
- Print the result by converting into an integer.

As we are using a function as key, we have to convert it to the key using cmp_to_key method from functools. Let's see the code.

from functools import cmp_to_key # initializing the list numbers = [45, 35, 138, 43, 67] def get_key(first, second): if str(first) + str(second) > str(second) + str(first): return -1 return 1 # getting the result result = sorted(numbers, key=cmp_to_key(get_key)) # joining the result result = "".join(str(integer) for integer in result) # printing the result print(int(result))

If you run the above code, then you will get the following result.

67454335138

If you have any queries in the tutorial, mention them in the comment section.

- Related Questions & Answers
- Program to create largest lexicographic number from a list of numbers in C++
- Program to find largest distance pair from two list of numbers in Python
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- Largest even number possible by using one swap operation in given number in C++
- Largest N digit number divisible by given three numbers in C++
- Create list of numbers with given range in Python
- C++ Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
- Extract numbers from list of strings in Python
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Find the Number of Quadrilaterals Possible from the Given Points using C++
- Python program to find largest number in a list
- Python - Find the number of prime numbers within a given range of numbers
- Python Program to Print Largest Even and Largest Odd Number in a List
- Python Group Anagrams from given list

Advertisements