Python Basic Course (5 List Tuples)

list

Python Lists are of various types such as numbers, strings, lists, etc. in one list. Data can be stored. Both constants and variables can be elements.

l = ['a', 'b', 'c'] X = [1, 2, 3.4, "a", l]

X contains [1, 2, 3.4, "a", ['a','b','c']]. Enclose the list in [] and separate each element with ,. Copy the following program and execute it. I will explain each function.

py3:list_explain:py


z = ['a','b','c']
x = [1, 2, 3.4, "a", z]
a_in_list = 'a' in z
d_in_list = 'd' in z
print("a_in_list {0}".format(a_in_list))
print("d_in_list {0}".format(d_in_list))
print("len(z) {0}".format(len(z)))
print("max(z) {0}".format(max(z)))
print("min(z) {0}".format(min(z)))
print("x[1] {0}".format(x[1]))
print("x[-1] {0}".format(x[-1]))
print("x[1:3] {0}".format(x[1:3]))
print("x[1:] {0}".format(x[1:]))
print("x[:3] {0}".format(x[:3]))
x[1:4] = ["taro","jiro"]
print("changed x {0}".format(x))
a = z + x
print("a {0}".format(a))

y = [] #create empty list
y.append("spam")
print("y {0}".format(y))
y.append("ham")
print("y {0}".format(y))
y2 = ["egg","ham"]
y.append(y2)
y.remove('ham')
print("y {0}".format(y))

z.reverse()
print("z {0}".format(z))

sortlist = [0,1,3,7,5]
sortlist.sort()
print("sort {0}".format(sortlist))

Check for the existence of elements in the list

Like the string type, the list can also be checked with ** x in list ** to see if the element exists in the list.

Indexing

Like the string type, the list can get the xth element.

len (list), max (list), min (list)

The same processing as a character string can be performed on an element.

Replace elements by specifying a range

x = [1, 2, 3.4, "a", z] Against x[1:4] = ["taro","jiro"] By writing [1, 'taro', 'jiro', ['a', 'b', 'c']] You can change the contents of the list in this way. The [1: 4] part of the original list [2, 3.4, "a"], that is, the three elements As a result of replacement, there are two ["taro", "jiro"]. This "replace element by specifying range" is possible for lists, but string type is not supported.

Add element

You can add elements to the list with ** List.append (element) **. Elements can also be lists. In this case, the process is to concatenate the list into the list. As you can see from the program, you can do the same with the symbol "+".

Delete element

You can remove an element from the list with ** List.remove (element) **. The list can store the same element multiple times, but if there are multiple specified elements Delete the first element. If you try to delete an element that does not exist, you will get an error. It is safe to do remove () after using in to make sure it exists in advance.

Reverse order of list

You can reverse the order of the elements in the list with ** List.reverse () **.

Sort list

You can sort the elements in the list in order with ** list.sort () **.

Tuple

Tuples are very similar to lists. Enclose it in () instead of [].

tuple = (1,2,3.4,"a","test")

You can check the existence of elements, indexing, len, min, max as in the list, but You cannot replace elements or add or delete elements. So is there any reason to use tuples whose elements cannot be changed?

The advantage of using tuples is that the program execution speed is a little faster. If you want to prepare a list whose contents do not change, you can also include tuples as an option. There are other benefits to using tuples, but we'll talk about that later.

Next: Python Basic Course (6 sets)

Recommended Posts

Python Basic Course (5 List Tuples)
Python basic course (12 functions)
Python Basic Course (7 Dictionary)
Python basic course (2 Python installation)
Python Basic Course (11 exceptions)
Python basic course (6 sets)
Python Basic Course (Introduction)
Python basic course (13 classes)
Python basic course (8 branches)
Python Basic Course (3 Python Execution)
Python basic course (10 inclusion notation)
Python list and tuples and commas
Python Basic Course (1 What is Python)
[Python] list
Python Basic Course (14 Modules and Packages)
Python basic operation 1st: List comprehension notation
Python basics: list
Basic Python writing
Basic grammar of Python3 series (list, tuple)
Python> Comprehension / Comprehension> List comprehension
Python3 basic grammar
Python Basic Course (at the end of 15)
RF Python Basic_02
Python basic course (4 numeric type / character string type)
Python list manipulation
How to clear tuples in a list (Python)
Sorted list in Python
Python Exercise 2 --List Comprehension
List of python modules
Python> list> extend () or + =
Python I'm also basic
Python basic grammar / algorithm
Python list comprehension speed
Filter List in Python
[python] class basic methods
python unittest assertXXX list
Python3 | Lists, Tuples, Dictionaries
Python> Tuples versus Lists
Python3 cheat sheet (basic)
Python lists, tuples, dictionaries
Python basic grammar (miscellaneous)
Python3 List / dictionary memo
[Memo] Python3 list sort
OpenCV3 Python API list
Python error list (Japanese)
Python basic memorandum part 2
python basic on windows ②
List find in Python
Python basic memo --Part 2
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Python basic grammar memo
OpenCV basic code (python)
Python basic memo --Part 1
Python exception class list
python memorandum super basic
Python basic if statement
How tuples work | Python
Initialize list with python