tag_ids = dict(conn.execute(tag_id_query).fetchall())
for tag in hashtags:
    if tag not in tag_ids:
        result = conn.execute(tags.insert(), {'tagname': tag})
Setting up/Inserting into Many-to-Many Database with Python, SQLALchemy, Sqlite