-2

I have a list of lists within which I want to search for a specific number to retrieve that specific list:

I have a list with values such as: [[266, 5300, 2696, 50.88], [36, 5191, 2938, 53.16], [875, 5133, 2553, 51.29]] and I want to get the list with the one that contains the number 875 and I want to retrieve the second (5133) and third (2553) number in that list. How do I search for the 875 within this list and return those two values? I cant index because the order changes every time but the id (875) will always be the first number in the list within a list.

I tried indexing but the order changes every time, I d'ont know how to search within the list to find a specific list starting with a number

Heres a snippet of the code but I have no clue where to start in terms of searching through the list:

id = {"id1": 887, "id2": 126}
url = "example.com"
# Send GET request to the URL
response = requests.get(url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
json_data = response.json()
# Pretty print the JSON data
wr_value = json_data['header']['wr']
vsRTop = json_data['top']

How would I parse through 'top' to get the values using id1 for example?

T K
  • 1
  • 1
  • We can't tell you what you did wrong without seeing your code... [ask] and [mre] – Julien May 25 '23 at 01:04
  • If you need to find something in a list, you have to iterate over the list. It doesn't matter that you're looking for a sublist or whatever. You might consider a dict instead, or if this is a web api then support requesting what you want. But without the ability to fix the underlying data/api then your only option is sequential search. I can't really elaborate without code, other than "what Julien said." – Kenny Ostrom May 25 '23 at 01:17
  • Welcome to Stack Overflow. I assume you know how to check what the first element of a list is, and then check whether that is the "specific number" you are interested in. Use this logic to search the list of lists, and the slice the values you want from the result - see the linked duplicates. Questions like this one are not appropriate for Stack Overflow, because they are straightforwardly about following a series of steps - we want questions that are about **one** thing. You [are expected](https://meta.stackoverflow.com/questions/261592) to try to *analyze* the task before posting. – Karl Knechtel May 25 '23 at 02:48

2 Answers2

-1

if i understood your question, then to search for a specific list within a list of lists based on a particular number, you need to iterate over each sub-list and check if the first element matches the desired number.

id = {"id1": 887, "id2": 126}
url = "example.com"
response = requests.get(url)

if response.status_code == 200:
  json_data = response.json()
  top_data = json_data['top']
  for item in top_data:
    if item[0] == id["id1"]:
      value1 = item[1]
      value2 = item[2]
-1

You can use a simple list comprehension:

out = [l[1:3] for l in vsRTop if l[0]==875]

Output:

[[5133, 2553]]

Or, if you just want the first match (if any), use a generator expression and next:

val1, val2 = next((l[1:3] for l in vsRTop if l[0]==875),
                  (None, None))

Output:

5133, 2553

Used input:

vsRTop = [[266, 5300, 2696, 50.88],
          [36, 5191, 2938, 53.16],
          [875, 5133, 2553, 51.29]]
mozway
  • 194,879
  • 13
  • 39
  • 75