Click here to Skip to main content
15,443,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me with below question in python

sample_list = ['', '',
               '', '',

I would want everything preceeding '', '','' and '' in list1 and corresponding prefixes in another list as:

result_list1 = ['Ironman.mdc', '', '', 'Antman',
                '', 'Captain']
result_list2 = ['', '', '',
                '', '', '']

What I have tried:

Tried this

result_list1 = [i.split('.')[0] for i in sample_list]

Did not get proper result
Updated 12-Mar-20 19:29pm

I think the only way to separate those parts would be by using a regular expression parser. Take a look at re — Regular expression operations — Python 3.7.7rc1 documentation[^] for some ideas.

It can be done with split but it requires a bit of calculation, as shown in the example below. You should be able to adapt this to your own requirements:
def combine(list, lstart, lend):
    for w in list[lstart:lend-1]:
        print(w, end='.')
    for w in list[lend-1:lend]:

list = ''.split('.')
right = len(list) - 2
combine(list, 0, right)
combine(list, right, len(list))

list = ''.split('.')
right = len(list) - 2
combine(list, 0, right)
combine(list, right, len(list))

Share this answer
Member 14767732 12-Mar-20 21:55pm     CRLF
Hi Richard Thanks you for the solution.It works brilliantly, but i have a problem in storing the values. I want to append the `(w,end=".")`and `w` to a list. Example, the list would be : `Names1=['','Antman']` I am getting only `['']` I am able to do for googlekey,net and but for the former one. Can you please help.
I got it , I did this:

def combine(temp1, lstart, lend):
    global y
    for w in temp1[lstart:lend-1]: 
    for w in temp1[lend-1:lend]:

for i in edge_names:
    right = len(temp1) - 2
    combine(temp1, 0, right)
    combine(temp1, right, len(temp1)) 

for a list of

It segregates the above into :


Thank you so much for your help. Appreciate it!
Share this answer
A clever RegEx can simplify your code much more.

will return a list of 2 which is: '' and ''
just have to add 'google' in front of second.

Another way is to use search() function with the RegEx '(.+)\.(google.+)', the result is 2 groups which are: '' and ''

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre -[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900