About python dict and sorted functions

When studying python, the sorted function is inevitable. Moreover, I often forget it, so make a note of it. This uses the sorted function for the dictionary. The point to keep in mind is that y and x after lambda can be anything else. For example, you can use lambda aaa: aaa [0]. Because y and x here are just the contents of the previous list (listA, listB, listC in the example). It seems that it's okay if you hold down even here. Take a look at the example below.

listA = [3, 6, 1, 0, 10, 8, 9]
print(sorted(listA))
#Result 1
#[0, 1, 3, 6, 8, 9, 10]

listB = ['g', 'e', 't', 'b', 'a']
print(sorted(listB))
print(sorted(listB, key=lambda y: y[0]))
#Result 2
#['a', 'b', 'e', 'g', 't']
#['a', 'b', 'e', 'g', 't']
listC = [('e', 4), ('o', 2), ('!', 5), ('v', 3), ('l', 1)]
print(sorted(listC, key=lambda x: x[1]))
#Result 3
#[('l', 1), ('o', 2), ('v', 3), ('e', 4), ('!', 5)]

** Difference between sorted and sorted: **

  1. sort is a function of list, while sorted can be used not only for list but also for all iterables such as dictionary dict.
  2. While sort manipulates the original list and transforms the list, sorted does not modify the original target, and there is a difference in the work flow of creating a new list and returning it. So be careful.

Well then again.

Recommended Posts

About python dict and sorted functions
Python 3 sorted and comparison functions
[Python] Memo about functions
A story about modifying Python and adding functions
About python objects and classes
About Python variables and objects
About Python, len () and randint ()
About Python datetime and timezone
About Python and regular expressions
About Python and os operations
Python higher-order functions and comprehensions
Python # About reference and copy
About Python sort () and reverse ()
About installing Pwntools and Python2 series
About dtypes in Python and Cython
Python functions
About Python pickle (cPickle) and marshal
[Python] About Executor and Future classes
About Python, from and import, as
[Python] Difference between sorted and sorted (Colaboratory)
About _ and __
Precautions when passing def to sorted and groupby functions in Python? ??
A story about Python pop and append
Talking about old and new Python classes
Use Python and MeCab with Azure Functions
Correspondence between Python built-in functions and Rust
Talking about Python class attributes and metaclasses
About python slices
About python comprehension
About Python tqdm.
About python yield
About python, class
About Go functions
About python inheritance
About python, range ()
Python Dict category
About python decorators
#Python basics (functions)
[Beginner] Python functions
About python reference
About Python decorators
Python Easy-to-use functions
[Python] About multi-process
Python basics: functions
Julia Quick Note [22] Calling Python functions and Python modules
Think about depth-priority and width-priority searches in Python
About the difference between "==" and "is" in python
Practice applying functions and global variables in Python
[Python] Learn about asynchronous programming and event loops
About the * (asterisk) argument of python (and itertools.starmap)
About shallow and deep copies of Python / Ruby
cv2 functions and data types (OpenCV python bindings)
Python> Sort by number and sort by alphabet> Use sorted ()
Sorted list in Python
[python] Compress and decompress
About Python for loops
Python Beginner's Guide (Functions)
Getting Started with python3 # 2 Learn about types and variables
About Class and Instance
Summary about Python scraping
Python basic course (12 functions)