Reputation: 839
How do I slice to get rid of "Hello", "World" and "Monty" in a list of lists? I have:
lst1 = [["Hello",1,2,3],["World",4,5,6],["Monty",7,8,9]]
I want:
lst2 = [[1,2,3],[4,5,6],[7,8,9]]
Upvotes: 1
Views: 2525
Reputation: 149756
You can get a slice of a list lst
starting with the second element using lst[1:]
. To do it for each sublist you can use a list comprehension:
>>> lst1 = [["Hello", 1, 2, 3], ["World", 4, 5, 6], ["Monty", 7, 8, 9]]
>>> lst2 = [lst[1:] for lst in lst1]
>>> lst2
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Upvotes: 3
Reputation: 10631
You can reach it with a list comprehension and get in each iteration the last elements beside the first of the nested list by using the [1:] selector.
lst1 = [["Hello", 1,2,3], ["World",4,5,6],["Monty",7,8,9]]
lst2 = [item[1:] for item in lst1]
print (lst2)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Upvotes: 3