Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use list in Python

Shulou Source: shulou.com Published: 2022-06-03 17:26:49 09月26日 Update

Editor to share with you how to use the list in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The sequence structures commonly used in python are: string, list, tuple, dictionary, collection.

1 list introduction

List: used to store any number of data sets of any type.

A list is a built-in variable sequence, an ordered and contiguous memory space containing multiple elements. Standard syntax format for list definitions:

A = [10, 10, 20, 30, 40]

Among them, 10, 20, 30, 40, these elements are called: list a.

The elements in the list can be different and can be of any type. For example: a = [10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10,

two。 Create a list

Basic syntax [] create > a = [10pc20 > a = [] # create an empty list object > a.append (20) > a [0] 20list () create # use list () to convert any iterable data into a list > a = list () # create an empty list object > > a = list (range (10)) > > a [0,1,2,3,4,5,6,7,8 9] > a = list ("gaoqi,sxt") > > a ['gathers,' asides, 'oaths,' qails, 'iTunes,', 'slots,' xbands,'t']

3. Add and delete

When the list adds and deletes elements, the list automatically manages memory, greatly reducing the burden on programmers. However, this feature involves a large number of movements of list elements and is inefficient.

Unless necessary, we usually only add or delete elements at the end of the list, which greatly improves the efficiency of the list.

> a = [20pr 40] > a.append (80) > a [20pr 40,80] 4. Deletion of list elements

Delete the element at the specified location in the list

> a = [100200888300400] > del a [1] > a [100200300400] 5. Sort the list > a = [20pint 10pint 30je 40] > id (a) 46017416 > > a.sort () # by default, it is in ascending order > > a = [10pence 20pct 30] > a.sort (reverse=True) # descending order > > a [40,30,20,10] > > import random > random.shuffle (a) # disrupt the order > > a [20,40,30,10] is all the contents of this article "how to use the list in Python"! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Elements articles memory content objects sequences efficiency data types syntax order same necessary less location ascending order multiple most dictionaries characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS NVidia MySQL vpn Apple