'Python create a sublist of list from a list
Here is my question: I have a list in python that looks like this:
list = [["a", "b", [1,2]], ["a", "C", [4,1,3]], ["e", "f", [1,7]]]
And I would like to get a sublist like this:
sublist = [[1,2], [4,1,3], [1,7]]
I would like to know if there is a quick way to make this transition,
Thank you in advance for your help :)
Solution 1:[1]
Don't use list
as a name for a list as it clashes with the built-in name. Use this:
l = [["a", "b", [1,2]], ["a", "C", [4,1,3]], ["e", "f", [1,7]]]
sublist = [i[-1] for i in l]
Output:
[[1, 2], [4, 1, 3], [1, 7]]
Solution 2:[2]
m = [i[2] for i in list]
print(m)
Output
[[1, 2], [4, 1, 3], [1, 7]]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abhyuday Vaish |
Solution 2 | inquirer |