code: def _construct_graph_from_dict_list(
      cls,
      vertices,
      edges,
      directed: bool = False,
      vertex_name_attr: str = "name",
      edge_foreign_keys=("source", "target"),
      iterative: bool = False,
  ):
      """Constructs a graph from a list-of-dictionaries representation.
  
      This function is useful when you have two lists of dictionaries, one for
      vertices and one for edges, each containing their attributes (e.g. name,
      weight). Of course, the edge dictionary must also contain two special keys
      that indicate the source and target vertices connected by that edge.
      Non-list iterables should work as long as they yield dictionaries or
      dict-like objects (they should have the 'items' and '__getitem__' methods).
      For instance, a database query result is likely to be fit as long as it's
      iterable and yields dict-like objects with every iteration.
  
      @param vertices: the list of dictionaries for the vertices or C{None} if
        there are no special attributes assigned to vertices and we
        should simply use the edge list of dicts to infer vertex names.
      @param edges: the list of dictionaries for the edges. Each dict must have
        at least the two keys specified by edge_foreign_keys to label the source
        and target vertices, while additional items will be treated as edge
        attributes.
      @param directed: whether the constructed graph will be directed
      @param vertex_name_attr: the name of the distinguished key in the
        dicts in the vertex data source that contains the vertex names.
        Ignored if C{vertices} is C{None}.
      @param edge_foreign_keys: tuple specifying the attributes in each edge
        dictionary that contain the source (1st) and target (2nd) vertex names.
        These items of each dictionary are also added as edge_attributes.
      @param iterative: whether to add the edges to the graph one by one,
        iteratively, or to build a large edge list first and use that to
        construct the graph. The latter approach is faster but it may
        not be suitable if your dataset is large. The default is to
        add the edges in a batch from an edge list.
      @return: the graph that was constructed
  
      Example:
  
      >>> vertices = [{'name': 'apple'}, {'name': 'pear'}, {'name': 'peach'}]
      >>> edges = [{'source': 'apple', 'target': 'pear', 'weight': 1.2},
      ...          {'source': 'apple', 'target': 'peach', 'weight': 0.9}]
      >>> g = Graph.DictList(vertices, edges)
  
      The graph has three vertices with names and two edges with weights.
      """
  
      def create_list_from_indices(indices, n):
          result = [None] * n
          for i, v in indices:
              result[i] = v
          return result
  
      # Construct the vertices
      vertex_attrs = {}
      n = 0
      if vertices:
          for idx, vertex_data in enumerate(vertices):
              for k, v in vertex_data.items():
                  try:
                      vertex_attrs[k].append((idx, v))
                  except KeyError:
                      vertex_attrs[k] = [(idx, v)]
              n += 1
          for k, v in vertex_attrs.items():
              vertex_attrs[k] = create_list_from_indices(v, n)
      else:
          vertex_attrs[vertex_name_attr] = []
  
      if vertex_name_attr not in vertex_attrs:
          raise AttributeError(
              f"{vertex_name_attr} is not a key of your vertex dictionaries",
          )
      vertex_names = vertex_attrs[vertex_name_attr]
  
      # Check for duplicates in vertex_names
      if len(vertex_names) != len(set(vertex_names)):
          raise ValueError("vertex names are not unique")
      # Create a reverse mapping from vertex names to indices
      vertex_name_map = UniqueIdGenerator(initial=vertex_names)
  
      # Construct the edges
      efk_src, efk_dest = edge_foreign_keys
      if iterative:
          g = cls(n, [], directed, {}, vertex_attrs)
          for idx, edge_data in enumerate(edges):
              src_name = edge_data[efk_src]
              dst_name = edge_data[efk_dest]
              v1 = vertex_name_map[src_name]
              if v1 == n:
                  g.add_vertices(1)
                  g.vs[n][vertex_name_attr] = src_name
                  n += 1
              v2 = vertex_name_map[dst_name]
              if v2 == n:
                  g.add_vertices(1)
                  g.vs[n][vertex_name_attr] = dst_name
                  n += 1
              g.add_edge(v1, v2)
              for k, v in edge_data.items():
                  g.es[idx][k] = v
  
          return g
      else:
          edge_list = []
          edge_attrs = {}
          m = 0
          for idx, edge_data in enumerate(edges):
              v1 = vertex_name_map[edge_data[efk_src]]
              v2 = vertex_name_map[edge_data[efk_dest]]
  
              edge_list.append((v1, v2))
              for k, v in edge_data.items():
                  try:
                      edge_attrs[k].append((idx, v))
                  except KeyError:
                      edge_attrs[k] = [(idx, v)]
              m += 1
          for k, v in edge_attrs.items():
              edge_attrs[k] = create_list_from_indices(v, m)
  
          # It may have happened that some vertices were added during
          # the process
          if len(vertex_name_map) > n:
              diff = len(vertex_name_map) - n
              more = [None] * diff
              for v in vertex_attrs.values():
                  v.extend(more)
              vertex_attrs[vertex_name_attr] = list(
                  vertex_name_map.values()
              )
              n = len(vertex_name_map)
  
          # Create the graph
          return cls(
              n, edge_list, directed, {}, vertex_attrs, edge_attrs
          )
  
file_path: python-igraph/src/igraph/io/objects.py
imports: from typing import Union, Sequence
  from collections import defaultdict
  from itertools import repeat
  from warnings import warn
  from igraph.datatypes import UniqueIdGenerator
label_desc: Use image node_id 1 for calling a global function with example usage: _construct_graph_from_dict_list(cls, vertices, edges, directed, vertex_name_attr, edge_foreign_keys, iterative) and returns: result, g, cls