1

I have a list such as this (this is pulled from some html texts using bs4):

test = ['Raji GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 'Comp Vortex GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 "Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price\n$70.00\nSale price$14.95\n\n                Save 79%",]

len(test) # 3

I want to loop through each line in the list (list of 3), and extract the 0,2nd and 4th indexed item for each line. so that the output looks like this:

This nested list contains all the items that I want to see.

out = [['Raji GlovesSixSixOneRegular price',
 'Sale price$9.95',
 '                Save 67%'],['Comp Vortex GlovesSixSixOneRegular price',
     'Sale price$9.95',
     '                Save 67%'],["Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price",
 'Sale price$14.95',
 '                Save 79%']]

I know that I can extract the items from the first line like this:

item1 = test[0]
item1 = item1.split(sep = '\n')
item1
indices = [0,2,4]
values =[]
for i in indices:
    print(item1[i])
    values.append(item1[i])
    
values 


['Raji GlovesSixSixOneRegular price',
 'Sale price$9.95',
 '                Save 67%']

I am new to python, and I struggling to pull out these items from each line, and append them back in a nested list (see out above).

Any idea's for how to achieve this?

3 Answers 3

3

Simple list comprehension

test = ['Raji GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 'Comp Vortex GlovesSixSixOneRegular price\n$29.99\nSale price$9.95\n\n                Save 67%\n              \nBUY 2 GET 1 FREE',
 "Shasta 3/4 Cycling Tights - Women'sSpecializedRegular price\n$70.00\nSale price$14.95\n\n                Save 79%",]

values = [i.split(sep = '\n')[0:5:2] for i in test]
Sign up to request clarification or add additional context in comments.

3 Comments

Wow that was so efficient! Thank you!
Could you please explain how the [0:5:2] part works?
Sure. This is the slice notation for python, and it works with the following format a[start:stop:step]. It is a small for loop for indexes of a list. So I start at 0, end at 5 and take a step of 2. Do note that stop is exclusive so I had to write 5, not 4. More details here stackoverflow.com/questions/509211/understanding-slice-notation and here docs.python.org/3/c-api/slice.html
2

What about something like:

out = []
indices = [0, 2, 4]

for elem in test:
    aux = []
    elem_split = elem.split('\n')
    for index in indices:
        item = elem_split[index]
        aux.append(item)
    out.append(aux)

1 Comment

Thank you! This is very easy to undertand
0

You can do it as below

values = []
for item1 in test:
    item1 = item1.split(sep = '\n')
    indices = [0,2,4]
    value = []
    for i in indices:
        value.append(item1[i])
    values.append(value)
print(values)

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.