[Python] I was hooked for an hour trying to use list comprehensions

Introduction

While studying Python, I was addicted to the list comprehension notation for an hour. I think it's because I thought Python functions were passed by reference. (Although I may have misunderstood "pass by reference")

If you compare the execution results, you can see what happened, I'm sure.

Addictive code and execution results

I created the opening function findDiff () to try to find out what's added and what's removed. But when I returned from the function, the elements of the list disappeared (looked like).

def findDiff(oldList, newList, adds, dels):
	adds = [x for x in newList if oldList.count(x) < 1]
	dels = [x for x in oldList if newList.count(x) < 1]

oldList = [1,2,3,5,6,7,8,9]
newList = [1,2,4,5,6,7,8,10]
adds = []
dels = []

print(adds)
print(dels)
findDiff(oldList, newList, adds, dels)
print(adds)
print(dels)

#>>> []
#>>> []
#>>> []
#>>> []

Modified code and execution result

Only the function findDiff () at the beginning is modified.

def findDiff(oldList, newList, adds, dels):
	for x in newList:
		if oldList.count(x) < 1:
			adds.append(x)
	for x in oldList:
		if newList.count(x) < 1:
			dels.append(x)

oldList = [1,2,3,5,6,7,8,9]
newList = [1,2,4,5,6,7,8,10]
adds = []
dels = []

print(adds)
print(dels)
findDiff(oldList, newList, adds, dels)
print(adds)
print(dels)

#>>> []
#>>> []
#>>> [4, 10]
#>>> [3, 9]

At the end

When I looked it up after writing the article, I already had the wisdom of my predecessors. .. ..

Recommended Posts

[Python] I was hooked for an hour trying to use list comprehensions
[Python] How to use list 1
I was hooked for 2 minutes with the Python debugger pdb
I didn't know how to use the [python] for statement
[Python] How to use list 3 Added
An introduction to Python for non-engineers
How to use an external editor for Python development with Grasshopper
Python> Comprehension> cells> I was taught how to use double comprehension / itertools
I want to use jar from python
[Python] Organizing how to use for statements
How to use "deque" for Python data
An introduction to Python for machine learning
Summary of how to use Python list
What I was addicted to Python autorun
An introduction to Python for C programmers
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
Tips for manipulating numpy.ndarray from c ++ -I want to use an iterator-
Use Python from Java with Jython. I was also addicted to it.
A memorandum because I stumbled on trying to use MeCab in Python
[Python] It was very convenient to use a Python class for a ROS program.
I want to use MATLAB feval with python
Study from Python Hour7: How to use classes
I was able to recurse in Python: lambda
Python list comprehensions that are easy to forget
I want to use Temporary Directory with Python2
[Algorithm x Python] How to use the list
I want to use ceres solver from python
I tried to build an environment for machine learning with Python (Mac OS X)
[BigQuery] How to use BigQuery API for Python -Table creation-
[For beginners] How to use say command in python!
I tried to summarize how to use matplotlib of python
I wanted to use the Python library from MATLAB
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to implement an artificial perceptron with python
[Python] I want to make a nested list a tuple
I tried to summarize how to use pandas in python
Python: Get a list of methods for an object
I was able to repeat it in Python: lambda
I made an action to automatically format python code
I was addicted to trying logging.getLogger in Flask 1.1.x
I want to use the R dataset in python
What I was addicted to when using Python tornado
As for the process type written in honcho's Procfile, I was addicted to it for over an hour because I couldn't use-, so I'll write it as a show.
How to use list []
python> link> Mid-line comment in Python?> I was told that it's better to use named arguments.
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
[Python] How to make an adjacency matrix / adjacency list [Graph theory]
[python] How to use the library Matplotlib for drawing graphs
[python] A note when trying to use numpy with Cython
How to make a Python package (written for an intern)
[Road to intermediate Python] Use if statement in list comprehension
How to use machine learning for work? 03_Python coding procedure
Useful tricks related to list and for statements in Python
[Python3] List of sites that I referred to when I started Python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
[Fixed] I was addicted to alphanumeric judgment of Python strings
[Python] I tried to explain words that are difficult for beginners to understand in an easy-to-understand manner.
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
Python list, for statement, dictionary
python3: How to use bottle (2)