Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access list of dictionaries
#4
I don't understand your posts. Are you making or querying a database? Maybe you should post some more code to provide context.

If you are querying a database, the query returns the columns as part of the description. Using the cars database created by my previous post:
import sqlite3 as sql

conn = sql.connect("cars.db")
cars = conn.cursor().execute("SELECT * FROM makes")
columns = [car[0] for car in cars.description]
print(columns)
print(*list(cars), sep="\n")
conn.close
Output:
['index', 'make', 'wheels', 'colour'] (0, 'Ford', '4', 'black') (1, 'Mercedes', '4', 'white') (2, 'Robin', '3', 'rusty yellow')
If you are creating a database, you should not use individual variables to represent a collection. Your code should not have ford or reliant or audi. You should have a list of cars, or better yet you should read a table of cars from a CSV file or have a GUI that allows entering cars. As far as the program is concerned, all cars are the same.
Reply


Messages In This Thread
Access list of dictionaries - by britesc - Jul-24-2023, 10:52 AM
RE: Access list of dictionaries - by deanhystad - Jul-24-2023, 02:55 PM
RE: Access list of dictionaries - by britesc - Jul-24-2023, 03:00 PM
RE: Access list of dictionaries - by deanhystad - Jul-24-2023, 07:23 PM
RE: Access list of dictionaries - by Pedroski55 - Jul-26-2023, 05:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 764 Apr-29-2024, 04:38 PM
Last Post: Calab
  function that returns a list of dictionaries nostradamus64 2 1,886 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,996 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,437 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  Help accessing elements of list of dictionaries Milfredo 6 2,987 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,231 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  how does .join work with list and dictionaries gr3yali3n 7 3,568 Jul-07-2020, 09:36 PM
Last Post: bowlofred
  access dictionary with keys from another and write values to list redminote4dd 6 3,411 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  creating a list of dictionaries from API calls AndrewEnglsh101 5 3,238 Apr-03-2020, 02:21 PM
Last Post: AndrewEnglsh101
  Access list items in Python kamaleon 2 2,467 Dec-31-2019, 11:10 AM
Last Post: kamaleon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020