-1

A basic error I know, but i'm unsure why its occurring after troubleshooting.

I have all_specs a list of lists:

[['2020 (70 reg)', 'SUV', '34,181 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (69 reg)', 'SUV', '43,243 miles', '400PS', 'Automatic'], ['2019 (19 reg)', 'SUV', '62,300 miles', '400PS', 'Automatic', 'Electric', '1 owner'], ['2018 (68 reg)', 'SUV', '26,850 miles', '400PS', 'Automatic', 'Electric', 'Full dealership history'], ['2020 (20 reg)', 'SUV', '46,000 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '34,000 miles', '400PS', 'Automatic', 'Electric'], ['2019 (68 reg)', 'SUV', '31,559 miles', '395BHP', 'Automatic', 'Electric'], ['2018 (68 reg)', 'SUV', '70,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2019 (19 reg)', 'SUV', '30,000 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (19 reg)', 'SUV', '48,800 miles', '400PS', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '18,043 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (69 reg)', 'SUV', '39,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2022 (22 reg)', 'SUV', '13,733 miles', '299PS', 'Automatic', 'Electric', '1 owner'], [], ['2019 (69 reg)', 'SUV', '24,418 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '35,700 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (19 reg)', 'SUV', '22,611 miles', '400PS', 'Automatic', 'Electric', 'Full service history'], ['2019 (69 reg)', 'SUV', '39,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2020 (20 reg)', 'SUV', '41,434 miles', '400PS', 'Automatic'], ['2019 (69 reg)', 'SUV', '41,019 miles', '400PS', 'Automatic', 'Electric'], ['2018 (18 reg)', 'SUV', '30,688 miles', '400PS', 'Automatic', 'Electric'], ['2019 (19 reg)', 'SUV', '26,393 miles', '400PS', 'Automatic', 'Electric'], ['2018 (68 reg)', 'SUV', '21,038 miles', '400PS', 'Automatic', 'Electric'], ['2019 (19 reg)', 'SUV', '64,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2020 (70 reg)', 'SUV', '34,600 miles', '400PS', 'Automatic', 'Electric', '2 owners'], ['2019 (69 reg)', 'SUV', '43,243 miles', '400PS', 'Automatic'], ['2020 (20 reg)', 'Saloon', '22,691 miles', '241BHP', 'Automatic', 'Electric']]

I am trying to create a list containing the first element of each of the sublists above. Using this Stackoverflow question here I've tried the following:

all_ages = [item[0] for item in all_specs]

But that is generating the error:

IndexError: list index out of range

I'm unsure why, because if I do a print of all_specs it appears to work:

[print(item[0]) for item in all_specs]

Outputs:

2020 (70 reg) 2019 (69 reg) 2019 (19 reg) 2018 (68 reg) 2020 (20 reg) 2020 (20 reg) 2019 (68 reg) 2018 (68 reg)

Curious Student
  • 669
  • 4
  • 13
  • 25

3 Answers3

0

I try to run [print(item[0]) for item in all_specs] and got the same error as all_ages = [item[0] for item in all_specs]

2020 (70 reg)
2019 (69 reg)
2019 (19 reg)
2018 (68 reg)
2020 (20 reg)
2020 (20 reg)
2019 (68 reg)
2018 (68 reg)
2019 (19 reg)
2019 (19 reg)
2020 (20 reg)
2019 (69 reg)
2022 (22 reg)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_15660\744302428.py in <module>
----> 1 [print(item[0]) for item in all_specs]

~\AppData\Local\Temp\ipykernel_15660\744302428.py in <listcomp>(.0)
----> 1 [print(item[0]) for item in all_specs]

IndexError: list index out of range

So please check your result. and here is the code could solve your question:

all_ages = [item[0] for item in all_specs if len(item)>0]

Because there is a empty list in all_specs

Kaison L
  • 86
  • 5
0

Like the other 2 users commented, there is an empty list in there. One I wanted to outline a potential way to debug it and then give the solution.

List comprehention (i.e. what you did) is a computationally more efficient thing to do. But for the same of debugging you can do something like below on a Jupyter Notebook.

for n, item in enumerate(all_specs):
    item[0]

You will get the error as expected. Then in a new block do the following

print(n, item)

This will give you

(13, [])

So you now know there is an empty list on the 13th index which is the culprit. So the fix would be to either clean the data or edit your list comprehension to the below version like Matthias said

[item[0] for item in all_specs if item]

PS: When you do it with print(item[0]), the error still happens. It just prints the first 13 entries before throwing an error.

Abilash
  • 95
  • 10
  • How would you insert some blank values where the list is empty? I have a for loop which creates all_specs: for spec in specs: if len(specs) == 0: car_specs.append('BLANK') else: entry = spec.get_attribute('innerHTML') entry = spec.text car_specs.append(entry) – Curious Student Jun 05 '23 at 13:21
  • The best way would be to fix it when you are making all specs. So if empty, you could do something like all_specs.append(['BLANK']). That way what was [ ] before becomes ['BLANK']. What you suggested makes [ ] into 'BLANK' which will not have an error but will give you unexpected results. – Abilash Jun 05 '23 at 14:18
  • @CuriousStudent did this work for you? If so could you accept the solution – Abilash Jun 12 '23 at 13:04
0

Try :

all_specs=[['2020 (70 reg)', 'SUV', '34,181 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (69 reg)', 'SUV', '43,243 miles', '400PS', 'Automatic'], ['2019 (19 reg)', 'SUV', '62,300 miles', '400PS', 'Automatic', 'Electric', '1 owner'], ['2018 (68 reg)', 'SUV', '26,850 miles', '400PS', 'Automatic', 'Electric', 'Full dealership history'], ['2020 (20 reg)', 'SUV', '46,000 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '34,000 miles', '400PS', 'Automatic', 'Electric'], ['2019 (68 reg)', 'SUV', '31,559 miles', '395BHP', 'Automatic', 'Electric'], ['2018 (68 reg)', 'SUV', '70,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2019 (19 reg)', 'SUV', '30,000 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (19 reg)', 'SUV', '48,800 miles', '400PS', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '18,043 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (69 reg)', 'SUV', '39,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2022 (22 reg)', 'SUV', '13,733 miles', '299PS', 'Automatic', 'Electric', '1 owner'], [], ['2019 (69 reg)', 'SUV', '24,418 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2020 (20 reg)', 'SUV', '35,700 miles', '395BHP', 'Automatic', 'Electric', '1 owner'], ['2019 (19 reg)', 'SUV', '22,611 miles', '400PS', 'Automatic', 'Electric', 'Full service history'], ['2019 (69 reg)', 'SUV', '39,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2020 (20 reg)', 'SUV', '41,434 miles', '400PS', 'Automatic'], ['2019 (69 reg)', 'SUV', '41,019 miles', '400PS', 'Automatic', 'Electric'], ['2018 (18 reg)', 'SUV', '30,688 miles', '400PS', 'Automatic', 'Electric'], ['2019 (19 reg)', 'SUV', '26,393 miles', '400PS', 'Automatic', 'Electric'], ['2018 (68 reg)', 'SUV', '21,038 miles', '400PS', 'Automatic', 'Electric'], ['2019 (19 reg)', 'SUV', '64,000 miles', '400PS', 'Automatic', 'Electric', '1 owner', 'Full dealership history'], ['2020 (70 reg)', 'SUV', '34,600 miles', '400PS', 'Automatic', 'Electric', '2 owners'], ['2019 (69 reg)', 'SUV', '43,243 miles', '400PS', 'Automatic'], ['2020 (20 reg)', 'Saloon', '22,691 miles', '241BHP', 'Automatic', 'Electric']]
    
    
    [print(*item[0:1]) for item in all_specs]