Introduction to Python zero Basics - 13 - list in Python

Posted by dickey on Thu, 24 Feb 2022 00:05:31 +0100

What is a list

List is a very important data type in Python. Why is it so important? Because in our actual development process, list is a data structure that is often used. It is widely used because it takes up less space and wastes less memory space.

  • A list is a queue
  • It is not only a collection of various data, but also a data structure
  • A list is an ordered collection type with repeatable contents

The list is an ordered sequence. All elements in the list are placed in the middle of [] and separated by commas, for example:

  • [1, 2, 3], a list of 3 integers
  • ['a', 'b', 'c'], a list of 3 strings

Get the element at the specified position in the list through index [], for example:

>>> x = ['a', 'b', 'c']
>>> x[0]
'a'
>>> x[1]
'b'
>>> x[2]
'c'
  • In line 2, get the 0th element 'a' of list x
  • In line 4, get the first element 'b' of list x
  • In line 6, get the second element 'c' of list x

Tips: the index of the list can also start from the last bit. For example, x[-1] gets the last element of the list. The following chapters on common operations, common functions and common methods of lists will be introduced in detail. You can understand them now

Definition of list

  • In Python, list represents the data type of list, which can also be used to define a list
  • In Python, the elements of the list exist in a [], as shown below
  • In Python, a list is an unlimited length data structure (but you should avoid creating very large lists)
  • A list can contain different types of elements, but each element type is usually the same when used
names = list(["lily", "jack", "hanmeimei"])
ages = [17, 18, 17]
print(names[0] + "What is your age" + str(ages[0]))
print(type(names))

Type of list

  • List elements can be any data type or mixed
  • Arrays can be nested, commonly known as binary arrays
list1 = [1, 2, 3, 1.11]
list2 = ["a", "b", "c"]
list3 = [True, False]
list4 = [{"a": 123}, {"b": 456}]
list5 = [[1], [2]]
list6 = [(1), (2)]
list7 = [1, 1.2, "2", True, {}, [], ()]

Use of in, max and min in the list

Keyword in

Check whether the specified element is included in the list through the keyword in. An example is as follows:

>>> 'lily' in ["lily", "jack", "hanmeimei"]
True
>>> 'neo' in ["lily", "jack", "hanmeimei"]
False
  • In line 1, the detection string 'lily' is in the list
  • In line 3, the detection string 'neo' is not in the list

Max (list) function

Use the function max to get the largest element in the list. An example is as follows:

>>> max([1, 2])
2
>>> max([1, 3, 2])
3

Min (list) function

Use the function min to get the smallest element in the list. An example is as follows:

>>> min([1, 2])
1
>>> min([1, 3, 2])
1

It should be noted that when max and min are used in the list, the elements in the list cannot be of multiple types. If the types are not unified, an error will be reported.

Little exercise on list

none_list = [None, None, None]

print(none_list)
print(bool(none_list))
# >>>True

print(len(none_list))
# >>>3

print([])
print(bool([]))
# >>>False

max_list = [1, 3.14]
print(max_list)
print(max(max_list))
# >>>3.14

print(min(max_list))
# >>>1

In the following scenarios, the English scores of the current class are 100, 94, 98, 95, 97, 66, 87, 79, 99, 92, 88, 98.5, 95, 100, 77, 69, 86, 97, 79, 97, 99, 95, 66, 95, 98, 95.5 and 88.5 respectively

Please count how many students there are at present, and what are the highest and lowest scores respectively?

en_score_list = [100, 94, 98, 95, 97, 66, 87, 79, 99, 92, 88, 98.5, 95, 100,
                 77, 69, 86, 97, 79, 97, 99, 95, 66, 95, 98, 95.5, 88.5]

stu_amount = len(en_score_list)
max_score = max(en_score_list)
min_score = min(en_score_list)
print("There are students in the current class " + str(stu_amount) + " people")
print("The highest score in English is " + str(max_score) + " branch")
print("The lowest score in English is " + str(min_score) + " branch")
# >>>There are 27 students in the current class
# >>>The highest score of English is 100
# >>>The lowest score of English is 66

What if there are different data types in a list? What about using the max function for lists of different data types? Let's look at the following example

a = [1, 2, True, 3]
print(max(a))
# >>> 3

a = ["a", "b", "c"]
print(max(a))
# >>> c

a = [[1, 2], [3, 4], [3, 5]]
print(max(a))
# >>> [3, 5]
a = ["a", "b", 1]
print(max(a))


# >>>Output results
# >>> print(max(a))
# >>> TypeError: '>' not supported between instances of 'int' and 'str'
  • Obviously, a direct error is reported. The prompt > operator does not support the use of int and str data types
  • Therefore, when using the max and min functions, remember that the data in the list is of the same data type

Topics: Python Back-end list