-1

I have a list of dicts like this:

    [{'sftp_conn': 'conn_1', 'sftp_path': 'path_1'}, 
     {'sftp_conn': 'conn_2', 'sftp_path': 'path_2'}
    ]

I know how to fetch values from the list of values

I want to loop each in the list and assign values to the variables

  id =1   sftp_conn = 'conn_1' , sftp_path = 'path_1'

then next

  id=2  sftp_conn = 'conn_2' , sftp_path = 'path_2'

I have an Airflow code as below, so I want to iterate and pass the values to sftp_path and sftp_conn_id

    for count, sftp in enumerate(sftp_list):
                s3_to_sftp = S3ToSftpOperator(
                    task_id=f's3_to_sftp_{count}',
                    sftp_path=  ,
                    sftp_conn_id= ,
                    s3_conn_id=S3_CONN_ID,
                )
Y4RD13
  • 937
  • 1
  • 16
  • 42
Kar
  • 790
  • 13
  • 36

2 Answers2

1

You just need to enumerate over the list while passing start=1 to start counting from 1 instead of 0. Please let me know if this is not what you meant.

li = [{'sftp_conn': 'conn_1', 'sftp_path': 'path_1'},{'sftp_conn': 'conn_2', 'sftp_path': 'path_2'}]

for item in enumerate(li, start=1):
    taskID, sftp_conn, sftp_path = item[0], item[1]['sftp_conn'], item[1]['sftp_path']
    #print(taskID, sftp_conn, sftp_path)

When that print line is uncommented, this will be the output for above list:

1 conn_1 path_1
2 conn_2 path_2
Zircoz
  • 504
  • 3
  • 14
0

You can easily iterate over it and use values() dict method.

d = [{'sftp_conn': 'conn_1', 'sftp_path': 'path_1'}, 
    {'sftp_conn': 'conn_2', 'sftp_path': 'path_2'}
    ]
    
x = [(' '.join(i.values())).split(' ') for i in d]
v1, v2, v3, v4 = x[0][0], x[0][1], x[1][0], x[1][1]
Y4RD13
  • 937
  • 1
  • 16
  • 42
  • I want to pass the values to the variable example ` sftp_conn = conn_1 `and `sftp_path = path_1` for first list then iterate to second list – Kar Jul 14 '20 at 16:39
  • great, Also I need to enumerate values as we have in list enumerate which gives 0 ,1 – Kar Jul 14 '20 at 16:43
  • @Kar I don't know If I'm understanding correctly, would you like to assign values to each dictionary? Or do you want to name each dictionary? – Y4RD13 Jul 14 '20 at 16:46
  • Updated the original question with detailed – Kar Jul 14 '20 at 16:49