Python Forum
Creating a list of dictionaries while iterating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a list of dictionaries while iterating
#2
Let's figure out the structure you want. The "director_known_for" bit is right now a list, with odd elements. Maybe instead of a list you want another dict, so you can index by director? Then those values will be another dict? Is the stuff after "director1" supposed to be the value? If so, then (with some f-string reformatting), maybe this is what you're looking for?

tmdb_url = "https://themoviedb.org/movie"
director_known_for = {
                       director1: {movie1: f"{k['title']} {k['release_date']} {tmdb_url} {k['id']}",
                                   movie1: f"{k['title']} {k['release_date']} {tmdb_url} {k['id']}",
                                  },
                     }
Why are the elements like k['title'] used repeatedly? Since k isn't changing, this will be the same in all the items.
Reply


Messages In This Thread
RE: Creating a list of dictionaries while iterating - by bowlofred - Sep-26-2020, 09:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 707 Apr-29-2024, 04:38 PM
Last Post: Calab
  Access list of dictionaries britesc 4 1,194 Jul-26-2023, 05:00 AM
Last Post: Pedroski55
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,991 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Delete list while iterating shantanu97 1 1,929 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  function that returns a list of dictionaries nostradamus64 2 1,832 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,933 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating list of lists from generator object t4keheart 1 2,250 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a dictionary from a list Inkanus 5 3,252 Nov-06-2020, 06:11 PM
Last Post: DeaD_EyE
  Help accessing elements of list of dictionaries Milfredo 6 2,954 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,198 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

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