{"id":10389,"date":"2021-09-30T16:30:19","date_gmt":"2021-09-30T11:00:19","guid":{"rendered":"https:\/\/python-programs.com\/?p=10389"},"modified":"2021-11-22T18:34:24","modified_gmt":"2021-11-22T13:04:24","slug":"python-program-to-find-the-largest-number-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-largest-number-in-a-list\/","title":{"rendered":"Python Program to Find the Largest Number in a List"},"content":{"rendered":"
Lists in Python:<\/strong><\/p>\n Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable.<\/p>\n Given a list, the task is to find the largest number in the given list in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to find the largest element\/item in the given list some of them are:<\/p>\n Want to excel in java coding? Practice with these Java Programs examples with output<\/a> and write any kind of easy or difficult programs in the java language<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation: Output:<\/strong><\/p>\n This is the easiest and fastest approach.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This is the efficient way to find the largest element in the given list.<\/p>\n This is the easiest and fastest approach.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Lists in Python: Lists are one of Python’s most commonly used built-in data structures. You can make a list by putting all of the elements inside square brackets[ ] and separating them with commas. Lists can include any type of object, making them extremely useful and adaptable. Given a list, the task is to find …<\/p>\ngiven list =[1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82, 82, 189, 274, 284, 467, 677,\r\n 871, 873, 876, 947, 1838, 2954, 5123]<\/pre>\n
The largest element present in the given list [1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82,\r\n82, 189, 274, 284, 467, 677, 871, 873, 876, 947, 1838, 2954, 5123] = 5123<\/pre>\n
given list =[1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34]<\/pre>\n
The largest element present in the given list [1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34] = 34<\/pre>\n
Program to Find the Largest Number in a List in Python<\/h2>\n
\n
Method #1:Using sort() function in Ascending Order(Static Input)<\/h3>\n
\n
# Given the list as static input and store it in a variable.\r\ngvnList = [12, 34, 11, 9, 2, 4, 1, 23, 28,\r\n 29, 25, 23, 31, 18, 15, 8, 13, 15, 17]\r\n# Sort the given list using the sort() function\r\n# (which sorts the elements in ascending order).\r\ngvnList.sort()\r\n# Calculate the length of the given list using the len() function\r\n# and store it in a variable.\r\nlstLeng = len(gvnList)\r\n# Print the largest\u00a0element in the given list with the help of the length of the\r\n# list variable(The last element can be found at the index length of list -1)\r\nlargstele = gvnList[lstLeng-1]\r\nprint('The largest element present in the given list', gvnList, '=', largstele)\r\n<\/pre>\n
The largest element present in the given list [1, 2, 4, 8, 9, 11, 12, 13, 15, 15, 17, 18, 23, 23, 25, 28, 29, 31, 34] = 34<\/pre>\n
Method #2:Using sort() function in Ascending Order(User Input)<\/h3>\n
\n
# Given the list as user input separated by spaces\r\n# using map(),split(),list() and int functions.\r\n# Store the list in one variable.\r\ngvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))\r\n# Sort the given list using the sort() function\r\n# (which sorts the elements in ascending order).\r\ngvnList.sort()\r\n# Calculate the length of the given list using the len() function\r\n# and store it in a variable.\r\nlstLeng = len(gvnList)\r\n# Print the largest\u00a0element in the given list with the help of the length of the\r\n# list variable(The last element can be found at the index length of list -1)\r\nlargstele = gvnList[lstLeng-1]\r\nprint('The largest element present in the given list', gvnList, '=', largstele)\r\n<\/pre>\n
Enter some random elements to the list separated by spaces = 36 18 1 34 9 7 64 15 284 1838 189 873 52 47 18 \r\n65 677 876 2954 26 82 871 467 274 5123 82 947 27 47 18 1 4 3 5 2\r\nThe largest element present in the given list [1, 1, 2, 3, 4, 5, 7, 9, 15, 18, 18, 18, 26, 27, 34, 36, 47, 47, 52, 64, 65, 82,\r\n82, 189, 274, 284, 467, 677, 871, 873, 876, 947, 1838, 2954, 5123] = 5123\r\n\r\n<\/pre>\n
Method #3:Using sort() function in Descending Order(User Input)<\/h3>\n
\n
\n<\/strong><\/p>\n# Given the list as user input separated by spaces\r\n# using map(),split(),list() and int functions.\r\n# Store the list in one variable.\r\ngvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))\r\n#Sort the given list in descending order using the sort() \r\n#and reverse=True(which sorts the elements in descending order).\r\ngvnList.sort(reverse=True)\r\n\r\n#Print the largest element in the given list by printing the first\r\n#element in the given list(which is having an index of 0)\r\nlargstele = gvnList[0]\r\nprint('The largest element present in the given list', gvnList, '=', largstele)\r\n<\/pre>\n
Enter some random elements to the list separated by spaces = 36 282 194 196 578 1 2 34 727 84\r\nThe largest element present in the given list [727, 578, 282, 196, 194, 84, 36, 34, 2, 1] = 727<\/pre>\n
Method #4:Using max() function(Static Input)<\/h3>\n
\n
# Given the list as static input and store it in a variable.\r\ngvnList = [49, 189, 18, 9, 10, 2, 49, 1, 38, 29]\r\n# Print the maximum element in the given by\r\n# providing the given list as an argument to max() function.\r\nlargstele = max(gvnList)\r\nprint('The largest element present in the given list', gvnList, '=', largstele)\r\n<\/pre>\n
The largest element present in the given list [49, 189, 18, 9, 10, 2, 49, 1, 38, 29] = 189<\/pre>\n
Method #5:Using max() function(User Input)<\/h3>\n
\n
# Given the list as user input separated by spaces\r\n# using map(),split(),list() and int functions.\r\n# Store the list in one variable.\r\ngvnList = list(map(int, input('Enter some random elements to the list separated by spaces = ').split()))\r\n# Print the maximum element in the given by\r\n# providing the given list as an argument to max() function.\r\nlargstele = max(gvnList)\r\nprint('The largest element present in the given list', gvnList, '=', largstele)\r\n<\/pre>\n
Enter some random elements to the list separated by spaces = 46 28 1 937 472 8545 28 1 83 94 28 95 27 58\r\nThe largest element present in the given list [46, 28, 1, 937, 472, 8545, 28, 1, 83, 94, 28, 95, 27, 58] = 8545<\/pre>\n
\n