[Introduction to Python] What is the difference between a list and a tuple?

I will talk about the difference between "list" and "tuple" that I stumbled upon when I first started Python.

Originally learning C in the early days, I was initially confused by the variety of arrays. If you understand each specification, you will not waste time, so please know it.

Python has similar data management methods called "lists" and "tuples". However, there are subtle differences in the element rewriting and code writing methods, so I would like to explain the differences.

What is a list?

A data type for storing multiple elements in order.

How to describe

list1.py


$ a = [1,2,3,4,5]
$ a
[1,2,3,4,5]

In this way, write the elements side by side in [].

How to handle elements

These elements can be rewritten.

list2.py


$ a = [1,2,3,4,5] #Define a list
$ a[1] #Show second element
2
$ a[1]="two" #The second element"two"Rewrite to
$ a
[1, 'two', 3, 4, 5]

What is a tuple?

It's basically the same as a list. However, as mentioned at the beginning, there are differences in how elements are handled and how code is written.

How to describe

tuple1.py


$ b = (1,2,3,4,5)
$ b
(1,2,3,4,5)

Note that the element was described in () this time, although it was [] in the previous list.

How to handle elements

Let's rewrite the elements as in the list.

tuple2.py



$ b=(1,2,3,4,5) #Define tuple
$ b
(1, 2, 3, 4, 5)
$ b[1] #Show second element
2
$ b[1]="two" #The second element"two"Rewrite to
Traceback (most recent call last):

  File "<ipython-input-9-6d86f5f6feb4>", line 1, in <module>
    b[1]="two"

TypeError: 'tuple' object does not support item assignment

I tried it, but an error occurred. As you can see, ** tuples cannot rewrite elements. ** **

You can concatenate tuples to create a new tuple just like a list. However, it is possible because it does not change the tuple itself.

tuple2.py


$ b2 = b + (11,12) #Connect tuples
$ b2
(1, 2, 3, 4, 5, 11, 12)

Why can't I rewrite

It is a tuple with less freedom than the list, but it exists because it has the advantage of not being rewritable. When dealing with important data that should not be changed, put it in a tuple so that it will not be changed.

Summary

It's a small difference, but if you don't know it, you'll get stuck in the acupoints. However, if you know it, there will be no problem. By the way, there are more types of arrays. There are also five types: list type, tuple type, array type, dictionary type, set type, and array type. It's hard to learn how to use it with all this, so just keep in mind that ** different types can cause errors **. And if you get an error of unknown cause, suspect a difference in the type specifications. I think that is fine.

Thank you for reading until the end. Please do not hesitate to point out any mistakes.

Recommended Posts

[Introduction to Python] What is the difference between a list and a tuple?
What is the difference between a symbolic link and a hard link?
[Python Iroha] Difference between List and Tuple
[Introduction to Infectious Disease Models] What is the difference between the April epidemic and this epidemic? .. .. ‼
About the difference between "==" and "is" in python
What is the difference between Unix and Linux?
What is the difference between usleep, nanosleep and clock_nanosleep?
Difference between list () and [] in Python
Difference between == and is in python
[Introduction to Python] What is the recommended way to install pip, a package management system?
Difference between append and + = in Python list
[Introduction to Python] What is the most powerful programming language now?
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
[Introduction to statistics] What kind of distribution is the t distribution, chi-square distribution, and F distribution? A little summary of how to use [python]
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
[Python] What is a formal argument? How to set the initial value
The answer of "1/2" is different between python2 and 3
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Python] What is pip? Explain the command list and how to use it with actual examples
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
A python amateur tries to summarize the list ②
[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
[Introduction to Python] What is the method of repeating with the continue statement?
What is the fastest way to create a reverse dictionary in python?
[Python] I want to make a nested list a tuple
How to use argparse and the difference between optparse
Python list is not a list
[Python] Python and security-① What is Python?
[Introduction to Python] <list> [edit: 2020/02/22]
Difference between ps a and ps -a
What is a python map?
Differences in behavior between append () and "+ =" operators when adding data to a list in Python
How to check in Python if one of the elements of a list is in another list
[Introduction to Python] What is the important "if __name__ =='__main__':" when dealing with modules?
Understand the difference between cumulative assignment to variables and cumulative assignment to objects
List concatenation method in python, difference between list.extend () and “+” operator
Build a Python environment and transfer data to the server
How to get the last (last) value in a list in Python
I tried to enumerate the differences between java and python
Extract the value closest to a value from a Python list element
[Python] What is a zip function?
[Python] What is a with statement?
Difference between java and python (memo)
[Introduction to Python3 Day 1] Programming and Python
[Python] What is @? (About the decorator)
[python] What is the sorted key?
Recursively get the Excel list in a specific folder with python and write it to Excel.
Difference between python2 series and python3 series dict.keys ()
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
What is the python underscore (_) for?
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
[Introduction to Python] How to split a character string with the split function
How to give and what the constraints option in scipy.optimize.minimize is
[Introduction to Python] I compared the naming conventions of C # and Python.
[Introduction to Python] How to use the in operator in a for statement?
Difference in how to write if statement between ruby ​​and python
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)