List operation memo writing

Write notes for list operations

Create Dict from two lists

>>> a = ["a", "b", "c"]
>>> b = [1,   2,   3]
>>> dict(zip(a,b))
{'a': 1, 'c': 3, 'b': 2}

Leave only the necessary elements from the List

>>> #Leave only 5 or more elements
>>> a = [3, 6, 4, 7, 2, 10]
>>> filter(lambda x: x >= 5, a)
[6, 7, 10]
>>>
>>> #Same as the bracket below
>>> fn = lambda x: x >= 5
>>> [i for i in a if fn(i)]
[6, 7, 10]

Sequential execution

>>> #Add 5 to all numbers
>>> a = [3, 6, 4, 7, 2, 10]
>>> map(lambda x: x + 5, a)
[8, 11, 9, 12, 7, 15]
>>>
>>> #Same as the bracket below
>>> fn = lambda x: x + 5
>>> [fn(i) for i in a]
[8, 11, 9, 12, 7, 15]

Fit everything inside the list

>>> #Add all
>>> a = [3, 6, 4, 7, 2, 10]
>>> reduce(lambda x,y: x+y, a)
32

sort

>>> #ascending order
>>> a = [3, 6, 4, 7, 2, 10]
>>> sorted(a)
[2, 3, 4, 6, 7, 10]

Recommended Posts

List operation memo writing
List memo writing part 2
List reverse operation
Python3 List / dictionary memo
[Memo] Python3 list sort
HTML / CSS memo writing
Python decorator operation memo
Pycharm setting memo writing
Operation of filter (None, list)
[Python] Operation memo of pandas DataFrame
Operation memo of Conda virtual environment
Python numbers, strings, list types (Python learning memo ①)
[Python beginner memo] Python character string, path operation