0

i have 4 lists The "name" List is the unique identifier for all the lists.I am clearing the data on lists Spec_titles,spec_details,varients over a for loop and appending new details on to them

name=["Some_car1","Some_car2",..."Some_carx"]
spec_titles=["price", "engine", "bhp","doors" ]
spec_details=["40000$", "1200cc", "125bhp", 5]
varients=["standard","deluxe","premium"]

i want to combine all these lists in a json format like below

{"vehicledetails":[

  { "Some_car1": {
    "price":"40000$",
    "engine":"1200cc",
    "bhp":"125bhp",
    "doors":"5",
    "varients":"[
        "standard",
        "deluxe",
        "premium"   ]"},
  {"Some_car2":[details]},
  .
  .
  {"Some_carx":[details]}]}

i have tried using list-comprehension but couldnot add list varients items into the json, please help me with this problem Thanks in Advance

4
  • 1
    Do all cars have the same details? Commented Mar 29, 2020 at 16:42
  • no ,diffrent cars have diffrent details, some may have 50 specs_details and 50 spec_titles while some may have 25 spec_details and 25 spec_titles. spec_details and spec_titles will be same number for a car Commented Mar 29, 2020 at 16:43
  • 1
    so where or how would wee know which data some_car1 gets and what data some_car2 gets? Commented Mar 29, 2020 at 16:43
  • like in the list spec_titles will be holding the what specification it holds and spec_details will be holding the corresponding detail of the specification. like spec_title[0]=spec_details[0]. and i am clearing the details on each list on a for loop and appending new details onto it Commented Mar 29, 2020 at 16:48

1 Answer 1

1

Based on your comment that you are clearing the spec_details in each loop an repopulating it for new cars you could do something like this. I have populated the spec_details are random for n cars

import json
from random import randint

spec_titles = ["price", "engine", "bhp", "doors"]
varients = ["standard", "deluxe", "premium"]

cars = {}
vehical_data = {"vehicaldetails": [cars]}
for n in range(5):
    name = "some_car" + str(n)
    spec_details = [str(randint(10000, 40000)) + "$", str(randint(600, 1200)) + "cc", str(randint(50, 150)) + "bhp", str(randint(3, 5))]
    car_data = dict(zip(spec_titles, spec_details))
    car_data["varients"] = varients
    cars[name] = car_data

print(json.dumps(vehical_data))

OUTPUT - json pretty format

{
   "vehicaldetails":[
      {
         "some_car0":{
            "price":"27982$",
            "engine":"848cc",
            "bhp":"90bhp",
            "doors":"5",
            "varients":[
               "standard",
               "deluxe",
               "premium"
            ]
         },
         "some_car1":{
            "price":"28400$",
            "engine":"1147cc",
            "bhp":"149bhp",
            "doors":"3",
            "varients":[
               "standard",
               "deluxe",
               "premium"
            ]
         },
         "some_car2":{
            "price":"17148$",
            "engine":"1079cc",
            "bhp":"109bhp",
            "doors":"5",
            "varients":[
               "standard",
               "deluxe",
               "premium"
            ]
         },
         "some_car3":{
            "price":"10831$",
            "engine":"991cc",
            "bhp":"90bhp",
            "doors":"3",
            "varients":[
               "standard",
               "deluxe",
               "premium"
            ]
         },
         "some_car4":{
            "price":"38619$",
            "engine":"1000cc",
            "bhp":"63bhp",
            "doors":"3",
            "varients":[
               "standard",
               "deluxe",
               "premium"
            ]
         }
      }
   ]
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.