Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8155157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:35:18+00:00 2026-06-06T16:35:18+00:00

I am using Python 3.2.3, and would like to find the largest number from

  • 0

I am using Python 3.2.3, and would like to find the largest number from specific elements in a list. It is also important that I retain some knowledge of which element is the largest number, but am quite flexible on how this occurs. Let me explain…

Background

I have the following list:

the_list = ['Order', '1', '5', 'Order', '2', '18', 'Order', '3', '45', 'Order', '4', '2', 'Order', '5', '8', 'Order', '6', '2', 'Order', '7', '1', 'Order', '8', '1', 'Order', '9', '1']

Out of every three list elements, the first two are descriptive of the data — i.e., “Order 1”, “Order 2”, “Order 3” as ‘Order’, ‘1’, … ‘Order’, ‘2’, … ‘Order’, ‘3’, … all the way to “Order 9”. These do not change, and provide the source or name of every third list element.

The third of every three list elements is the information in question. In this example, the numbers are 5… 18… 45… 2… and so forth. It from this every third element that I would like to find the largest number. In this case, that largest number is 45. These numbers change all the time; they could be any whole number from 0 through 100 [inclusive].

What I Have Tried So Far

I have tried using Python’s max() function in two ways. First, simply…

max(the_list)

… which provides “Order” as the maximum value. A bummer on my goal.

So I decided to try making a new list, made only of every third element of the original list. Like so…

foo = (the_line[2], the_line[5], the_line[8], the_line[11], the_line[14], the_best_line[17], the_best_line[20], the_best_line[23], the_best_line[26])

max(foo)

… which provides “8” as the largest number, the 15th list element and the 5th third list element. It’s a number, but isn’t the highest number of 45 [in this example].

I have also dabbled in making a series of if-else statements, but was both unsuccessful and in the mind-set that there must be a more pythonic//elegant way. I admit that I may have quit too early on this route, and//or that I am in the wrong mind-set.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:35:19+00:00Added an answer on June 6, 2026 at 4:35 pm

    Something like this should work:

    >>> the_list = ['Order', '1', '5', 'Order', '2', '18', 'Order', '3', '45', 'Order', '4', '2', 'Order', '5', '8', 'Order', '6', '2', 'Order', '7', '1', 'Order', '8', '1', 'Order', '9', '1']
    >>> the_list[::3]
    ['Order', 'Order', 'Order', 'Order', 'Order', 'Order', 'Order', 'Order', 'Order']
    >>> the_list[2::3]
    ['5', '18', '45', '2', '8', '2', '1', '1', '1']
    >>> max(int(num) for num in the_list[2::3])
    45
    

    Where I’ve used Python’s slice notation to get every third (that’s the 3) element starting at element #2 (that’s the 2), using the_list[2::3].

    By itself that’s not enough, though, because the entries of the_list are strings and they’re sorted lexicographically, not numerically. That’s why I had to call int(num) on each of the terms and pass max a generator expression, here in the form ‘(something for elem in someseq)’.

    You also say that it’s important that you keep track of which element is the largest, by which I assume you mean you want the index. Given the maximum value it’s easy to find which elements have it (in the general case it might not be unique, after all) using another search for elements that match it which is probably the simplest. Alternatively, you could encode the index itself into the max call:

    >>> max((int(num), i) for i, num in enumerate(the_list[2::3]))
    (45, 2)
    

    which gives the max and the group-of-three index, and works because tuples are sorted by the first element, then the second, etc. This approach doesn’t handle non-unique maxima as well.

    To be honest, though, I’d probably start by reshaping the data kind of like @astynax did– it doesn’t feel like this list should be flat.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Python I would like to find the date object for last Wednesday. I
I would like to run a process from Python (2.4/2.5/2.6) using Popen , and
I would like to find the relatedness (not similarity) between two words using Python.
I would like to parse free-text time intervals like the following, using Python: 1
I have a working application using python and zeromq and I would like to
I'm using python Textile to store markup in the database. I would like to
I'm using the python suds module and would like to retrieve the response headers
Background I would like my Python script to pause before exiting using something similar
Let's assume I'm stuck using Python 2.6, and can't upgrade (even if that would
I have a csv DictReader object (using Python 3.1), but I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.