Getting list elements in Python

1. Get the specified element. (Specify index)

$ list = ['a', 'b', 'c', 'd', 'e']

list[0]
# 'A'

list[1]
# 'B'

list[2]
# 'C'

list[-1]
# 'E'

list[-2]
# 'D'

2. Get part of the list. (Slicing)

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']

3. Get it with a query. (Inclusive notation)

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

Remarks

This article is a migration article from the blog "Technical notes for chores engineers". The previous blog will be deleted.

Recommended Posts

Getting list elements in Python
Delete multiple elements in python list
Sorted list in Python
Filter List in Python
List find in Python
Randomly select elements from list (array) in python
Sort list elements in a specified order in Python
[Python] Manipulating elements in a list (array) [Sort]
How to remove duplicate elements in Python3 list
[Python] Manipulation of elements in list (array) [Add / Delete]
[Python] Outputs all combinations of elements in the list
[Python] list
Group by consecutive elements of a list in Python
Extract multiple list duplicates in Python
python web scraping-get elements in bulk
[python] Manage functions in a list
Output 2017 Premium Friday list in Python
Make sure all the elements in the list are the same in Python
Sort and output the elements in the list as elements and multiples in Python.
Get the number of specific elements in a python list
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Python basics: list
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Display a list of alphabets in Python 3
OR the List in Python (zip function)
Discord in Python
DCI in Python
quicksort in python
Difference between append and + = in Python list
[Python] Display list elements with variadic arguments
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Download images from URL list in Python
Lifegame in Python.
Sqlite in python
Python> Comprehension / Comprehension> List comprehension
StepAIC in Python
Summary of built-in methods in Python list
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Get the EDINET code list in Python
Quad-tree in Python