How to sort a list of lists by a specific index of the inner list?
I have a list of lists. For example,
[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]
If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?
python sorting
add a comment |
I have a list of lists. For example,
[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]
If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?
python sorting
6
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
3
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52
add a comment |
I have a list of lists. For example,
[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]
If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?
python sorting
I have a list of lists. For example,
[
[0,1,'f'],
[4,2,'t'],
[9,4,'afsd']
]
If I wanted to sort the outer list by the string field of the inner lists, how would you do that in python?
python sorting
python sorting
edited Nov 13 '10 at 22:00
John La Rooy
211k39276429
211k39276429
asked Nov 13 '10 at 21:54
oldspiceoldspice
993387
993387
6
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
3
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52
add a comment |
6
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
3
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52
6
6
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
3
3
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52
add a comment |
9 Answers
9
active
oldest
votes
This is a job for itemgetter
>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
It is also possible to use a lambda function here, however the lambda function is slower in this simple case
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
@bzupnick, usekey=lambda x:x[2].casefold()
. If your Python isn't new enough, just use.lower()
instead of.casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
|
show 2 more comments
in place
>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])
not in place using sorted:
>>> sorted(l, key=lambda x: x[2])
2
Could you give more details aboutin place
andnot in place
?
– qun
Oct 10 '15 at 15:42
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
add a comment |
Itemgetter lets you to sort by multiple criteria / columns:
sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
add a comment |
Like this:
import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))
add a comment |
multiple criteria can also be implemented through lambda function
sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))
add a comment |
I think lambda function can solve your problem.
old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])
#Resulst of new_list will be:
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
add a comment |
array.sort(key = lambda x:x[1])
You can easily sort using this snippet, where 1 is the index of the element.
add a comment |
More easy to understand (What is Lambda actually doing):
ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
#return the third item of the list
return ls[2]
#Sort according to what the thirdItem function return
ls2.sort(key=thirdItem)
add a comment |
**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])**
correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4174941%2fhow-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is a job for itemgetter
>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
It is also possible to use a lambda function here, however the lambda function is slower in this simple case
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
@bzupnick, usekey=lambda x:x[2].casefold()
. If your Python isn't new enough, just use.lower()
instead of.casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
|
show 2 more comments
This is a job for itemgetter
>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
It is also possible to use a lambda function here, however the lambda function is slower in this simple case
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
@bzupnick, usekey=lambda x:x[2].casefold()
. If your Python isn't new enough, just use.lower()
instead of.casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
|
show 2 more comments
This is a job for itemgetter
>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
It is also possible to use a lambda function here, however the lambda function is slower in this simple case
This is a job for itemgetter
>>> from operator import itemgetter
>>> L=[[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> sorted(L, key=itemgetter(2))
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
It is also possible to use a lambda function here, however the lambda function is slower in this simple case
edited Nov 13 '10 at 22:13
answered Nov 13 '10 at 21:59
John La RooyJohn La Rooy
211k39276429
211k39276429
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
@bzupnick, usekey=lambda x:x[2].casefold()
. If your Python isn't new enough, just use.lower()
instead of.casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
|
show 2 more comments
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
@bzupnick, usekey=lambda x:x[2].casefold()
. If your Python isn't new enough, just use.lower()
instead of.casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
What if I would want to ignore case?
– bzupnick
Jul 29 '13 at 11:31
5
5
@bzupnick, use
key=lambda x:x[2].casefold()
. If your Python isn't new enough, just use .lower()
instead of .casefold()
– John La Rooy
Jul 29 '13 at 11:52
@bzupnick, use
key=lambda x:x[2].casefold()
. If your Python isn't new enough, just use .lower()
instead of .casefold()
– John La Rooy
Jul 29 '13 at 11:52
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is can we sort using itemgetter() with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
Can I also get the indices of the sort, so to sort another related list of lists in the same order?
– quarky
Aug 22 '17 at 8:52
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
@quaryk It sounds like an interesting question, but not suitable to answer in the comments. If you can't find a question that covers it, you should create one.
– John La Rooy
Aug 23 '17 at 0:13
|
show 2 more comments
in place
>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])
not in place using sorted:
>>> sorted(l, key=lambda x: x[2])
2
Could you give more details aboutin place
andnot in place
?
– qun
Oct 10 '15 at 15:42
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
add a comment |
in place
>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])
not in place using sorted:
>>> sorted(l, key=lambda x: x[2])
2
Could you give more details aboutin place
andnot in place
?
– qun
Oct 10 '15 at 15:42
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
add a comment |
in place
>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])
not in place using sorted:
>>> sorted(l, key=lambda x: x[2])
in place
>>> l = [[0, 1, 'f'], [4, 2, 't'], [9, 4, 'afsd']]
>>> l.sort(key=lambda x: x[2])
not in place using sorted:
>>> sorted(l, key=lambda x: x[2])
edited Nov 13 '10 at 22:06
answered Nov 13 '10 at 22:00
mouadmouad
46.1k139395
46.1k139395
2
Could you give more details aboutin place
andnot in place
?
– qun
Oct 10 '15 at 15:42
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
add a comment |
2
Could you give more details aboutin place
andnot in place
?
– qun
Oct 10 '15 at 15:42
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
2
2
Could you give more details about
in place
and not in place
?– qun
Oct 10 '15 at 15:42
Could you give more details about
in place
and not in place
?– qun
Oct 10 '15 at 15:42
7
7
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
@qun, "in place" means that the memory of the old list is reused for the sorted one. "not in place" means that the old list remains unchanged and a new list is created.
– John La Rooy
Oct 15 '15 at 6:15
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
x = [[[5,3],1.0345],[[5,6],5.098],[[5,4],4.89],[[5,1],5.97]] With a list like this is, how can we sort with respect to elements in x[0][1] ?
– nidHi
Dec 2 '16 at 9:48
add a comment |
Itemgetter lets you to sort by multiple criteria / columns:
sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
add a comment |
Itemgetter lets you to sort by multiple criteria / columns:
sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
add a comment |
Itemgetter lets you to sort by multiple criteria / columns:
sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))
Itemgetter lets you to sort by multiple criteria / columns:
sorted_list = sorted(list_to_sort, key=itemgetter(2,0,1))
answered Mar 21 '13 at 10:25
fiderfider
1,1801520
1,1801520
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
add a comment |
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
3
3
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
I think this answer is very important. I think people trying to sort by inner array indexes will fall here but people looking to sort by MULTIPLE inner array indexes will start here and your answer helped me see that itemgetter will actually do that for you!
– ZekeDroid
Oct 9 '14 at 16:55
add a comment |
Like this:
import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))
add a comment |
Like this:
import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))
add a comment |
Like this:
import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))
Like this:
import operator
l = [...]
sorted_list = sorted(l, key=operator.itemgetter(desired_item_index))
answered Nov 13 '10 at 22:00
Jim BrissomJim Brissom
20.4k23032
20.4k23032
add a comment |
add a comment |
multiple criteria can also be implemented through lambda function
sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))
add a comment |
multiple criteria can also be implemented through lambda function
sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))
add a comment |
multiple criteria can also be implemented through lambda function
sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))
multiple criteria can also be implemented through lambda function
sorted_list = sorted(list_to_sort, key=lambda x: (x[1], x[0]))
answered Mar 24 '17 at 9:46
Rahul KumarRahul Kumar
79210
79210
add a comment |
add a comment |
I think lambda function can solve your problem.
old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])
#Resulst of new_list will be:
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
add a comment |
I think lambda function can solve your problem.
old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])
#Resulst of new_list will be:
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
add a comment |
I think lambda function can solve your problem.
old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])
#Resulst of new_list will be:
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
I think lambda function can solve your problem.
old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])
#Resulst of new_list will be:
[[9, 4, 'afsd'], [0, 1, 'f'], [4, 2, 't']]
answered Aug 28 '17 at 13:26
Tushar NirasTushar Niras
821914
821914
add a comment |
add a comment |
array.sort(key = lambda x:x[1])
You can easily sort using this snippet, where 1 is the index of the element.
add a comment |
array.sort(key = lambda x:x[1])
You can easily sort using this snippet, where 1 is the index of the element.
add a comment |
array.sort(key = lambda x:x[1])
You can easily sort using this snippet, where 1 is the index of the element.
array.sort(key = lambda x:x[1])
You can easily sort using this snippet, where 1 is the index of the element.
answered Jun 20 '18 at 11:22
Abhishek YadavAbhishek Yadav
19019
19019
add a comment |
add a comment |
More easy to understand (What is Lambda actually doing):
ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
#return the third item of the list
return ls[2]
#Sort according to what the thirdItem function return
ls2.sort(key=thirdItem)
add a comment |
More easy to understand (What is Lambda actually doing):
ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
#return the third item of the list
return ls[2]
#Sort according to what the thirdItem function return
ls2.sort(key=thirdItem)
add a comment |
More easy to understand (What is Lambda actually doing):
ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
#return the third item of the list
return ls[2]
#Sort according to what the thirdItem function return
ls2.sort(key=thirdItem)
More easy to understand (What is Lambda actually doing):
ls2=[[0,1,'f'],[4,2,'t'],[9,4,'afsd']]
def thirdItem(ls):
#return the third item of the list
return ls[2]
#Sort according to what the thirdItem function return
ls2.sort(key=thirdItem)
answered Nov 23 '18 at 18:31
Maz1978Maz1978
315
315
add a comment |
add a comment |
**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])**
correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?
add a comment |
**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])**
correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?
add a comment |
**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])**
correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?
**old_list = [[0,1,'f'], [4,2,'t'],[9,4,'afsd']]
#let's assume we want to sort lists by last value ( old_list[2] )
new_list = sorted(old_list, key=lambda x: x[2])**
correct me if i'm wrong but isnt the 'x[2]' calling the 3rd item in the list, not the 3rd item in the nested list? should it be x[2][2]?
answered Jan 9 at 17:13
EgmontDeVosEgmontDeVos
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4174941%2fhow-to-sort-a-list-of-lists-by-a-specific-index-of-the-inner-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Link to tutorial: wiki.python.org/moin/HowTo/Sorting
– Felix Kling
Nov 13 '10 at 22:02
3
useful link: stackoverflow.com/questions/18142090/…
– dot.Py
Jun 6 '16 at 17:52