Home > Back-end >  split values in list and create 2 new list
split values in list and create 2 new list

Time:10-17

I have list = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD'].

I want to extract the separate list for /teams url and /service* url. And add the -1 index to the new list.

example:

INPUT list:
list = ['https://pagerduty.com/teams/PRJMI0O', 'https://pagerduty.com/services/PRWZID9', 'https://pagerduty.com/service-directory/PJNKJWD']

Expected OUTPUT
new_teams = ["PRJMI0O"]
new_service = ["PRWZID9", "PJNKJWD"]

please advice the logic.

CodePudding user response:

Hint : You can use the split function.

>>> 'https://pagerduty.com/teams/PRJMI0O'.split('/')
['https:', '', 'pagerduty.com', 'teams', 'PRJMI0O']

Answer : Let urls be the list you are concerned with.

new_teams = []
new_services = []
for url in urls:
    urlSplit = url.split('/')
    if 'teams' in urlSplit:
        new_teams.append(urlSplit[-1])
    if 'services' in urlSplit:
        new_services.append(urlSplit[-1])
  • Related