-1

I have a list which looks like this:

my_list = 
[['UK', 'Manchester City', 'Blue', '1','2','B'],
['ES', 'FC Barcelona', 'Blue', '2','1','C'], 
['IT', 'Juventus', 'White', '3','2','A'],
['DE', 'Borussia Dortmund', 'Yellow', '4','1','A']] 

Now I want to edit my_list and actually delete the values in index0,3,4 and 5.

This is my expected output:

  my_list = 
[['Manchester City', 'Blue'],
['FC Barcelona', 'Blue'], 
['Juventus', 'White'],
['Borussia Dortmund', 'Yellow']] 

This is the code I tried:

for list in my_list:
    for idx in [0,3,4,5]:
        del list[idx]       
        
print(my_list) 

This is the output I get:

IndexError: list assignment index out of range
NorthAfrican
  • 135
  • 2
  • 10

1 Answers1

2

Try

my_list = [['UK', 'Manchester City', 'Blue', '1', '2', 'B'],
           ['ES', 'FC Barcelona', 'Blue', '2', '1', 'C'],
           ['IT', 'Juventus', 'White', '3', '2', 'A'],
           ['DE', 'Borussia Dortmund', 'Yellow', '4', '1', 'A']]

new_list = [lst[1:3] for lst in my_list]
print(new_list)

output

[['Manchester City', 'Blue'], ['FC Barcelona', 'Blue'], ['Juventus', 'White'], ['Borussia Dortmund', 'Yellow']]
balderman
  • 22,927
  • 7
  • 34
  • 52