0

I have these two list group_name and group_ids_list im looping these two list and I want to do an if condition based on the group id of this list. I want to know if there is a way to do this if groupId == 0: dynamically because this current example wont scale very well if I keep adding more groups and so on.

group_name = ['a','a','b','c', 'a', 'd']
group_ids_list = [0,0,1,2,0,3]
for groupName, groupId in itertools.zip_longest(group_name, group_ids_list):
    if groupId == 0:
        print('found group 0 name is {}'.format(groupName))
    elif groupId == 1:
        print('found group 1 name is {}'.format(groupName))
    elif groupId == 2:
        print('found group 2 name is {}'.format(groupName))
3
  • 2
    Is group_ids really supposed to be the indexes or is it supposed to be the values found in group_ids_list? Commented Apr 15, 2020 at 4:17
  • I updated my code im actually looping two list and yes group id is associated to an id. if you see the group_name it coresponds to some value like a if in group 0 or b if in group 1 and so on. Commented Apr 15, 2020 at 4:22
  • it looks like you changed the question a lot since I answered so I deleted my answer. Commented Apr 15, 2020 at 4:28

1 Answer 1

1

If you want to just print the group id and name. You can use string formatting to achieve this.

group_names = ['a','a','b','c', 'a', 'd']
group_ids = [0, 0, 1, 2, 0, 3]
for group_name, group_id in zip(group_names, group_ids):
    print('found group {} with name {}'.format(group_id, group_name))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.