how to make a list of matching items from a list in python?

Viewed 47

i have a list of. i want the match items and print the match items in a list

assume that

dataReader= [
  {
    "id": 1,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
     "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"

  },
  {
    "id": 2,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
     "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"
 
  },
  {
    "id": 3,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
     "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"

  },
  {
    "id": 4,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
     "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"
  
  },
  {
    "id": 5,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
    "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"
  },
  {
    "id": 6,
    "user_payment_id": 2,
    "user_id": 1,
    "email": "string",
    "order_number": 53010,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string",
     "item_id": 1,
     "item_name": "string",
     "item_price": 0,
     "description": "string"
    
  }
]

i have this list of items. i want to filter the "order_number": 53010 match items from list and make another list of dictionaries of match item

i want the result like this

dataReader
 [
    "id": 1,
    "user_payment_id": 2, 
    "user_id": 1,
    "name": "string",
    "email": "string",
    "order_number": 53010 [
              {
      "item_id": 1,
      "item_name": "string",
      "item_price": 0,
      "item_description": "string"
      },
      {
      "item_id": 2,
      "item_name": "string",
      "item_price": 0,
      "item_description": "string"
      },
       {
      "item_id": 3,
      "item_name": "string",
      "item_price": 0,
      "item_description": "string"
      }
    ]
    "product_is_active": true,
    "billing_first_name": "string",
    "billing_last_name": "string",
    "billing_phone_number": "string",
    "billing_country": "string"
  }
]

i have write this code,

dataReader= [
      {
        "id": 1,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
    
      },
      {
        "id": 2,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
     
      },
      {
        "id": 3,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
    
      },
      {
        "id": 4,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
      
      },
      {
        "id": 5,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
      },
      {
        "id": 6,
        "user_payment_id": 2,
        "user_id": 1,
        "email": "string",
        "order_number": 53010,
        "billing_first_name": "string",
        "billing_last_name": "string",
        "billing_phone_number": "string",
        "billing_country": "string",
        "item_id": 1,
        "item_name": "string",
        "item_price": 0,
        "description": "string"
        
      }
    ]

count = 0
    
for row in dataReader:
       bookList= dict()
       #bookList[row[0]]= row[1]
       #print(row)
#for k,v in bookList.items():
            #print(k,v)
num_0f_rows=len(dataReader)
print("rows=",num_0f_rows)
row=0
if row< num_0f_rows:
        question_id_student_user=dataReader[row].get("order_number")
new_list=[]
for i in dataReader:
        if i["order_number"]==question_id_student_user:
                new_list.append(i)
                print(new_list)            
                
        #print(question_id_student_user)

i find the result but cannot save the result in a new list. the result is not showing.Help me out

1 Answers

You can filter a list of dict by key values by doing this:

filtered_list = [element for element in dataReader if element['order_number'] == 53010]
Related