
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python - Largest number possible from list of given numbers
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.
Example
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.
Output
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.
Example
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.
Output
67454335138
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- 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
- Largest even number possible by using one swap operation in given number in C++
- C++ Program to Generate All Possible Combinations of a Given List of Numbers
- C++ Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
- Python program to find largest number in a list
- Largest N digit number divisible by given three numbers in C++
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Create list of numbers with given range in Python
- Extract numbers from list of strings in Python
- Python program to find the largest number in a list
- Find the Number of Quadrilaterals Possible from the Given Points using C++
- Python program to find N largest elements from a list
- Python Group Anagrams from given list

Advertisements