Python3 comprehension (List, dictionary) that I have seen somewhere

Introduction

This is my memo for studying python Check the operation of the sample source with "Google Colaboratory".

List comprehension

Create a list with a for statement

test.py


list = [] 

for i in range(5):
    list.append(x)
print(list)

# => [0, 1, 2, 3, 4]

Create a list with comprehension

test.py


list = [i for i in range(5)] 
print(list)

# => [0, 1, 2, 3, 4]

What if can also be used

By adding if, the elements in List will be reduced. ** Supplement ** The if in the comprehension is called the if clause.

test.py


#[For statement] Put only even numbers in the List
list = [] 
for i in range(10):
    if i % 2 == 0:
        list.append(i)
print(list)
# => [0, 2, 4, 6, 8]

#[Comprehension notation] Put only even numbers in the List
list = [i for i in range(10) if i % 2 == 0] 
print(list)

# => [0, 2, 4, 6, 8]

You can also use if-else ... However, it is a mystery whether readability is good. .. ..

** Supplement ** If-else in the comprehension is called a conditional expression

test.py


#[For statement] Enter "even" and "odd"
list = [] 
for i in range(10):
    if i % 2 == 0:
        list.append("Even")
    else:
        list.append("Odd")
print(list)
# => ['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']

#[Comprehension] Enter "even" and "odd"
list = ["Even" if i % 2 == 0 else "Odd" for i in range(10)] 
print(list)

# => ['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']

Dictionary comprehension

Create a dictionary with a for statement

test.py


dict = {}
key_list = [1, 2, 3]
value_list = ["abc", "def", "xyz"]

for i in range(len(key_list)):
    key = key_list[i]
    value = value_list[i]
    dict[key] = value
print(dict) 
# => {1: 'abc', 2: 'def', 3: 'xyz'}

Create a dictionary with comprehension

test.py


key_list = [1, 2, 3]
value_list = ["abc", "def", "xyz"]

dict = {key : value for key, value in zip(key_list, value_list)}
print(dict)
# => {1: 'abc', 2: 'def', 3: 'xyz'}

At the end

Writing in comprehension will make it much shorter, but it's a little difficult to read until you get used to it.

When writing this When I was writing java as a subcontractor of an SI shop a long time ago, I couldn't read "ternary operator" and it would be shorter, so don't use it (angry) For some reason, I remembered what the prime contractor said.

*** Shortening ** ⇒ As you may know recently, the estimate of step unit price used to be ... (ry)

Acknowledgments

Thank you to shiracamus for commenting

Recommended Posts

Python3 comprehension (List, dictionary) that I have seen somewhere
Python> Comprehension / Comprehension> List comprehension
Python Exercise 2 --List Comprehension
Python list comprehension speed
Python3 List / dictionary memo
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
[Python] I tried to summarize the array, dictionary generation method, loop method, list comprehension notation
Python list, for statement, dictionary
python Condition extraction from a list that I often forget
[Python3] List of sites that I referred to when I started Python
[Python] I made a decorator that doesn't seem to have any use.
I measured the speed of list comprehension, for and while with python2.7.
Python basic operation 1st: List comprehension notation
Python comprehension (list and generator expressions) [additional]
I have a question! (Python, django) Easy
Note that Python decorators should have wraps
[Introduction to Udemy Python3 + Application] 61. Dictionary comprehension
Python comprehension
List comprehension
Python comprehension
List comprehension
[Python] list
Python dictionary
[Python] dictionary
Python dictionary
[Python / DynamoDB / boto3] List of operations I tried
[Python] List Comprehension Various ways to create a list
I made a python dictionary file for Neocomplete
A memo that I wrote a quicksort in Python
[Introduction to Udemy Python3 + Application] 60. List comprehension notation
Python list comprehensions that are easy to forget
Basic operation list of Python3 list, tuple, dictionary, set
I compared "python dictionary type" and "excel function"
Note that Python list comprehensions are always confusing
I checked the reference speed when using python list, dictionary, and set type in.