$ list = ['a', 'b', 'c', 'd', 'e']
list[0]
# 'A'
list[1]
# 'B'
list[2]
# 'C'
list[-1]
# 'E'
list[-2]
# 'D'
[Start number: End number: Number of steps]
. The end number element is not included.list = ['a', 'b', 'c', 'd', 'e']
list[3:]
# ['D', 'E']
list[:3]
# ['A', 'B', 'C']
list[::-1]
# ['E', 'D', 'C', 'B', 'A']
list[1:4:2]
# ['B', 'D']
list[4:1:-2]
# ['E', 'C']
[Element processing command for element name in list if extraction condition]
. Can be omitted after if.zip (List 1, List 2)
in the place of "in", each element is acquired by the combination of "Element in List 1, Element in the same position in List 2".{}
, it will be returned by the set or dictionary, and if you enclose it in ()
, it will be returned by the generator.list = ['a', 'b', 'c', 'd', 'e']
list2 = ['f', 'g', 'h', 'i', 'j']
[x.lower() for x in list]
# ['a', 'b', 'c', 'd', 'e']
[x.lower() for x in list if x!='B']
# ['a', 'c', 'd', 'e']
[str(i) + x for i, x in enumerate(list)]
# ['0A', '1B', '2C', '3D', '4E']
[x + y for x, y in zip(list, list2)]
# ['AF', 'BG', 'CH', 'DI', 'EJ']
{x.lower() for x in list}
# {'a', 'b', 'c', 'd', 'e'}※set
{x:x.lower() for x in list}
# {'A': 'a', 'B': 'b', 'C': 'c', 'D': 'd', 'E': 'e'}※dictionary
(x.lower() for x in list)
# <generator object <genexpr> at ?>※generator
This article is a migration article from the blog "Technical notes for chores engineers". The previous blog will be deleted.
Recommended Posts