## Python topic list type

Knowledge seeker 2020-11-13 12:02:42
python topic list type

# One Preface

official account ： Knowledge seeker

Knowledge seeker (Inheriting the spirit of open source, Spreading technology knowledge;)

# Two list

## 2.1 List introduction

The list is enclosed in square brackets 、 A collection of elements separated by commas ; Each element in the list has a fixed position , The index of the first element is 0, The index of the second element is 1, And so on ;

``````# Create a list of numbers
num = [2,5,8,10,13,15]
# 2
print(num)
# 5
print(num)
``````

Lists support slicing , Positive index from 0 Start , Reverse index from -1 Start ;

Positive order example

``````# Create a list of numbers
num = [2,5,8,10,13,15]
# Index from 0 To 2 barring 2
print(num[0:2])# [2, 5]
# Express Index from 0 To the end of the index
print(num[0:])# [2, 5, 8, 10, 13, 15]
``````

Reverse order example

``````# Create a list of numbers
num = [2,5,8,10,13,15]
# The reverse Index from -3 To -1( barring -1)
print(num[-3:-1])# [10, 13]
# The reverse Index from -3 To the end
print(num[-3:])#[10, 13, 15]
``````

Support shallow copy

``````# Create a list of numbers
num = [2,5,8,10,13,15]
# Shallow copy
print(num[:])#[2, 5, 8, 10, 13, 15]
``````

``````# Create a list of numbers
num = [2,5,8,10,13,15]
new_num = num + [66,100]
# [2, 5, 8, 10, 13, 15, 66, 100]
print(new_num)
``````

Support nested

``````# Create a list of numbers
num = [[2,5,8,10,13,15],[66,100]]
# [[2, 5, 8, 10, 13, 15], [66, 100]]
print(num)
``````

Support list elements as strings

``````# list present the fruit
fruit = ['apple','pear','strawberry']
``````

## 2.2 Update list elements

Update the elements directly at the index position of the list ;

``````fruit = ['apple','pear','strawberry']
fruit = 'banana'
# ['banana', 'pear', 'strawberry']
print(fruit)
``````

## 2.3 Delete list elements

Use del Keywords to delete

``````fruit = ['apple','pear','strawberry']
del fruit
# ['pear', 'strawberry']
print(fruit)
``````

# 3、 ... and List functions

## 3.1 len

len Function returns the number of elements in the list

``````fruit = ['apple','pear','strawberry']
# 3
print(len(fruit))
``````

## 3.2 max

max Function returns the maximum value of a list element

``````fruit = [2,5,8,10,13,15]
# 15
print(max(fruit))
``````

## 3.3 min

min function Returns the minimum value of a list element

``````fruit = [2,5,8,10,13,15]
# 2
print(min(fruit))
``````

## 3.4 list

Convert string to list

``````# ['h', 'a', 'p', 'p', 'y']
print(list('happy'))
``````

# Four Common functions

• list.append(obj) Add new elements at the end of the list
• list.count(obj) Count the number of times an element appears in a list
• list.extend(seq) Appends multiple values from another sequence at once at the end of the list （ Extend the original list with the new list ）
• list.index(obj) Find the index position of the first match of a value in the list
• list.insert(index, obj) Insert elements into the list
• list.pop([index=-1]) Removes an element from the list （ Default last element ）, And returns the value of that element
• list.remove(obj) Removes the first match of a value in the list
• list.reverse() Reverse list of elements
• list.sort( key=None, reverse=False) Sort the original list
• list.clear() clear list
• list.copy() Copy list

Examples are as follows , Other functions are used in a similar way

``````happy = ['h', 'a', 'p', 'p', 'y']
happy.append('today')
# ['h', 'a', 'p', 'p', 'y', 'today']
print(happy)
``````