0

I have multiple lists and I want to zip them like shown in the following example :

d = {} clients = ["client_1","client_2","client_3",...] # n number of client # every client has a list of element : d["acc_list" + client] = [1, 2, 3, ...] 

So how could I zip them without knowing the number of clients :

acc_clients = zip(d["acc_list" + "client_0"],d["acc_list" + "client_2"],d["acc_list" + "client_3"], .... ) 
5
  • 2
    What result do you want? Commented Apr 25, 2022 at 13:21
  • len(clients)? Commented Apr 25, 2022 at 13:22
  • I want to zip n number of lists, so instead of doing : zip(d["acc_list" + "client_1"],d["acc_list" + "client_2"],d["acc_list" + "client_3"] ) I want an efficient way to loop through clients list Commented Apr 25, 2022 at 13:26
  • like something like that : acc_clients = zip(d["acc_list" + client ] for client in client_names) but it doesnt work Commented Apr 25, 2022 at 13:27
  • Please provide a minimal reproducible example in your question and explain how it's not working. Commented Apr 25, 2022 at 13:30

1 Answer 1

1

So you already have the client part in client_names, so you can make a list of them:

client_d = [d["acc_list" + client ] for client in client_names] 

Now to zip them together you can apply to * operator to the list:

acc_clients = zip(*[d["acc_list" + client ] for client in client_names]) 

or as @wwii points out, we don't need to get a list comprehension to iterate over client_names first and then let * iterate over the result, we can make a generator expression:

acc_clients = zip(*(d["acc_list" + client ] for client in client_names)) 
Sign up to request clarification or add additional context in comments.

1 Comment

...zip(*(d["acc_list" + client ] for client in clients))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.