Python Forum
create dictionary from **kwargs that include tuple
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create dictionary from **kwargs that include tuple
#1
Hi,

I'm trying to initialise an object by passing a list of named tuples. I would like to build a dictionary.

the code that came as a result of a previous post is:
#!/usr/bin/python3
from collections import namedtuple

Edge = namedtuple("Edge", "v1 v2")

V = [Edge(1,2), Edge(2,3), Edge(3,1), Edge(4,1), Edge(2,4), Edge(4,5)]

Graph = {edge.v1: [] for edge in V}

for edge in V:
  Graph[edge.v1].append(edge.v2)

for x,y in Graph.items():
  print(" {} {} ".format(x,y))
I would now like to create a class and create a dictionary as part of __init__. Something like this:
#!/usr/bin/python3
from collections import namedtuple

Edge = namedtuple("Edge", "v1 v2")

class Graph:
  def __init__(self, *args):
    Graph = {edge.v1: [for edge in args] }

if __name__ == "__main__":
  V = [Edge(1,2), Edge(2,3), Edge(3,1), Edge(4,1), Edge(2,4), Edge(4,5)]
  g = Graph(V)
  start = 1
  end = 3
I however get the following error:
    Graph = {edge.v1: [for edge in args] }
                         ^
SyntaxError: invalid syntax
How can I therefore create a comprehension that creates a dictionary, based on a list of named tuples that appears as mapping of a key value pair, where the value is a list? Basically, it should appear as it does in the first snippet of code.
Reply
#2
Graph = {edge.v1: [for edge in args] }
in the 2nd code is not the same as
Graph = {edge.v1: [] for edge in V}
in the first code
Reply
#3
Hello,

I do exactly this in some code that I just posted on github today https://github.com/Larz60p/NLTK-Corpora-...oraData.py

There is a good example in fluent python (Luciano Ramalho - O'Reilly page 70)

from collections import defaultdict
DIAL_CODES = [(86, 'China'), (91, 'India'), (1, 'United States') ...]

country_code = {country: code for code, country in DIAL_CODES}
so in your case,
from collections import defaultdict

V = [('v1', (1,2)), ('v2', (2,3)), ('v3', (3,1)), ('v4', (4,1)), ('v5', (2,4)), ('v6', (4,5))]

Graph = {edge: tup for edge, tup in  V}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to include one script into another? MorningWave 8 305 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  how include a python code in notpad++ plugin akbarza 2 580 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Regex Include and Exclude patterns in Same Expression starzar 2 735 May-23-2023, 09:12 AM
Last Post: Gribouillis
  How do I instantiate a class with **kwargs? palladium 6 6,957 Feb-16-2023, 06:10 PM
Last Post: deanhystad
  Create new dictionary angus1964 6 2,013 May-08-2022, 12:43 PM
Last Post: snippsat
  Create new dictionary angus1964 2 1,223 May-06-2022, 02:14 PM
Last Post: deanhystad
  convert a named tuple to a dictionary Skaperen 13 3,387 Mar-31-2022, 07:13 PM
Last Post: Skaperen
  kwargs question Jeff_t 8 2,871 Feb-16-2022, 04:33 PM
Last Post: Jeff_t
  How to include input as part of variable name Mark17 4 2,444 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Create SQLite columns from a list or tuple? snakes 6 8,516 May-04-2021, 12:06 PM
Last Post: snakes

Forum Jump:

User Panel Messages

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