in Education by
I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the string field is unique. how can I sort them based on values, I can do the sort based on the keys? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You need an ordered data type to represent the sorted values (probably a list of tuples) as it's not possible to sort a directory because they are orderless whereas list and tuples aren't. Ex. import operator y = {2: 3, 4: 5, 5: 4, 3: 2, 1: 1} sorted_y = sorted(y.items(), key=operator.itemgetter(1)) A list of tuples, here sorted_x will be sorted by first element in each tuple dict(sorted_y) == y If you want to sort the keys instead of values, use import operator y = {2: 3, 4: 5, 5: 4, 3: 2, 1: 1} sorted_y = sorted(y.items(), key=operator.itemgetter(1)) Hope this helps. P.S. Only works in the Python versions below 3.0 Learn in detail about Python by enrolling in Intellipaat Python Course online and upskill.

Related questions

0 votes
    I want each item to be sorted by a specific property value from a list of dictionaries I have. Take the ... When sorted by name Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5}? ... use the "sorted" operator that returns tuples. Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    How can I check if a key exists in a directory in Python, before updating the value of the key. I tried this ... better way to do this? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I check the file going to be written exists in the directory, and if not , How can I create directory ... " flag for the same? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In Python What command can I use to find these: Location of file which I am executing, and Current directory ( ... script in Python) Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I sort a data.frame by multiple columns. E.x. I would like to sort the column q(descending) by column f (ascending) dd...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    How can I add a key in python directory after it's creation ? I already tried .add() method but it doesn't work. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    In the os module in Python, is there a way to find if a directory exists, something like: os.direxists(os ... in pseudocode True/False Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    How to remove an element from a list by index in Python, I tried list.remove method but it search the list and ... How can I do this? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    There is no explanation in python documentation whether parameters are passed by value or reference and why the ... actual reference? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How do I get a list of all files (and directories) in a given directory in Python? Select the correct answer from above options...
asked Feb 4, 2022 in Education by JackTerrance
0 votes
    How can I delete a file or a folder in Python? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    How can I convert a str from uppercase, or half uppercase to lowercase? E.x. "Decimeter" to "decimeter" Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    How can I get the number of elements of a list? Ex- items = [] items.append("one") items.append("two") items.append("three") Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    suppose I have passed : list=[] How can I check if a list is empty or not? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
...