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?

NewBee
  • 990
  • 1
  • 7
  • 26

3 Answers3

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]
Niteya Shah
  • 1,809
  • 1
  • 17
  • 30
  • Wow that was so efficient! Thank you! – NewBee Jan 14 '21 at 21:33
  • Could you please explain how the [0:5:2] part works? – NewBee Jan 14 '21 at 21:34
  • 1
    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 https://stackoverflow.com/questions/509211/understanding-slice-notation and here https://docs.python.org/3/c-api/slice.html – Niteya Shah Jan 14 '21 at 21:36
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)
Giovanni Rescia
  • 605
  • 5
  • 15
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)
maddy
  • 3
  • 2