Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the similarities and differences between Python lists, tuples, and dictionaries

Shulou Source: shulou.com Published: 2022-06-01 04:58:45 10月20日 Update

This article focuses on "what are the similarities and differences between Python lists, tuples, and dictionaries". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the similarities and differences between Python lists, tuples, and dictionaries?"

List: []

Features: modifiable

Basic operations:

Index []

Slice []

Append append

Delete del

Length len

Include in

Eg:

> name_list = ('hello','jacky','dick')

> delname_list [0]

Traceback (mostrecent call last):

File "", line 1, in

TypeError:'tuple' object doesn't support item deletion

> type (name_list)

> name_list = ['jacky','dick','hello']

> id (name_list)

35733128L

> name_list.append ('hu')

> name_list

['jacky',' dick','hello', 'hu']

> id (name_list)

35733128L

> delname_list [0]

> name_list

['dick',' hello','hu']

How to turn a list into a string:

> "_" .join (name_list)

'dick_hello_hu'

> len (name_list)

three

> name_list

['dick',' hello','hu']

> > "jacky" in name_list

False

Tuple: ()

Features: unmodifiable

Basic operations:

Index []

Slice []

Length len

Include in

['dick','hello',' hu'] list

('dick',' hello','hu') tuple

[

'dick', ('jacky','kirk')

]

The list contains two elements:

First: string

Second: tuple

Add, delete and modify: minimum unit

# slice, index (- 1), length, contain, loop

Str: re-open up memory space

List: use the original memory space after modification

Tuple: modification is not allowed

Name_list = ['liu','zhang','huang']

For ele in name_list

Print ele

# 1 minute ele = liu

# 2 dint ele = zhang

# 3 minute ele = huang

If ele = 'zhang':

Print 'sb'

# this cycle will not continue

Continue

If ele = 'liu':

Print 'find'

Break

# jump out of the whole cycle

# for

# continue

# break

While condition:

Print 'xxx'

Condition = true true: loop

Condition = false false: no loop

While True:

Print 'xxxx'

Flag = True

While flag:

Print 'jjjj'

Flag = False

Print 'vvvv'

Dict, dictionary, key-value pair

Person = {

"name": 'dick'

"age": 18

"gender": 'man'

}

# each element is a key-value pair (key and value)

The dictionary can also be found through the "index", corresponding to the keys of the dictionary:

Person ['name']

Dict_test.py:

Person = {

"name": "dick"

"age": 29

"gerder": 'man'

}

# person.keys () # all key are taken out and saved as a list

# person.values () # all value are taken out and saved as a list

# person.items () # all elements # all key-value pairs

For KJO v in person.items ():

Print k

Print v

Print'='

Execution result:

E:\ > pythondict_test.py

Gender

Man

=

Age

twenty-nine

=

Name

Dick

=

Dictionary disorder, special for loop

Keys ()-- > list

Values ()-- > list

Items ()-- > when only the for loop is used, the element is assigned

At this point, I believe you have a deeper understanding of "what are the similarities and differences between Python lists, tuples and dictionaries". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Dictionary loop element index similarities and differences condition length memory content basic operation character string feature space learning practical deeper minimum special two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker MariaDB macOS NVidia