Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access list of dictionaries
#2
Next time try reading the documentation. Python documentation is pretty good, and I don't think it is referenced as often as it should.

https://docs.python.org/3/tutorial/datas...ctionaries

Quote:Performing list(d) on a dictionary returns a list of all the keys used in the dictionary, in insertion order (if you want it sorted, just use sorted(d) instead). To check whether a single key is in the dictionary, use the in keyword.

columns = list(ford)

Even if you didn't read that particular document, I'm having a tough time thinking up how you could try "various methods" and fail. Especially after you mentioned this: "I can retrieve all the dictionaries data with a for items loop.". Here are methods that use a loop to extract the dictionary keys.
ford = {"make": "Ford", "wheels": "4", "colour": "black"}

columns = []
for item in ford.items():
    columns.append(item[0])
print(columns)

columns = []
for key, value in ford.items():
    columns.append(key)
print(columns)

columns = []
for key in ford.keys():
    columns.append(key)
print(columns)

columns = []
for key in ford:
    columns.append(key)
print(columns)

columns = [key for key in ford.keys()]
columns = [key for key in ford]
print(columns)

columns = list(ford)
print(columns)
Did I misread your post?

You should look at using Pandas. Pandas can create a table from your dictionaries and write the table to a database.
import pandas as pd
import sqlite3 as sql

cars = [
    {"make": "Ford", "wheels": "4", "colour": "black"},
    {"make": "Mercedes", "wheels": "4", "colour": "white"},
    {"make": "Robin", "wheels": "3", "colour": "rusty yellow"},
]

cars = pd.DataFrame(cars)
print(cars.columns)

conn = sql.connect("cars.db")
cars.to_sql("makes", conn, if_exists="replace")
conn.close
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,997 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,569 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