1

From a config file, I would like to iterate over a list that contains users, repositories, and a password and then assign those values to variables to create a repository object using github3.py.

The user and repo_name lists will not be equal length. For now, there's two users and several repos, but this can change over time. I've tried to use zip, but it is only working if the lists are the same length. I would like for it to keep the last value in the user list and map that with the last repo in the repo_name list (user2,repo3).

user = ["user1","user2"]
repo_name = ["repo1","repo2","repo3"]
password = "xxxxxxxxx"

The code below is where I would like to use those values.

from global_var import *
for users, repos in zip(user, repo_name)
    gh = login(users, password)
    repo = gh.repository(users, repos)
    list_all_prs()
DBS
  • 1,107
  • 1
  • 12
  • 24

4 Answers4

5

Assuming that the repository list will always be at least as long as the user list, then you can simply use itertools.zip_longest to zip them together:

for u, r in itertools.zip_longest(user, repo_name, fillvalue=user[-1]):
    ...

As you note, zip ends after the shortest iterable is exhausted. In contrast zip_longest will continue until the longest iterable is exhausted, using the given fillvalue (which defaults to None) when a value can no longer be taken from the shorter iterables.

So here, we can simply set fillvalue to be the last member of user to have it broadcast over the remaining repositories.

(In python 2.7 this iterator is named itertools.izip_longest)

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
0

Use a hash (dictionary in Python I think)

myHash = { 
    'user1': {
      'repos' : [],
      'password' : 'cat123'
  },
    'user2': {
      'repos' : [],
      'password' : 'cat123'
  },
}

than you can get the last repo with myHash['user1']['repos'][-1]

krc
  • 483
  • 1
  • 3
  • 12
0

Use the izip_longest method in the itertools module to zip over the longest iterable. This will return 'None' in cases where there is no value in the shorter list, so you would need to catch this in a function or loop.

import itertools as it

a = []

    for user,repo in it.izip_longest(users,repo_name):
        if user: a.append((user,repo))
        else:  a.append((users[-1],repo))  

a

Output: [('user1', 'repo1'), ('user2', 'repo2'), ('user2', 'repo3')]
Moe Chughtai
  • 384
  • 2
  • 13
0

I'd say the better way is to map each user to a repo in a dict, but the closest thing to what you want is something like this

##assuming repos.len() >= users.len() and users is not empty
password = XXX
while repos:
    if users:
        user = users.pop()
    gh = login(user, password)
    repo = gh.repository(user, repos.pop())
    list_all_prs()
Mr. Nun.
  • 775
  • 11
  • 29