Python sequence (list, tuple, dictionary) additions and deletions

Source: Internet
Author: User
Tags stdin

List

Operation

List

Method

Example

Increase

List.append (obj)
add element to end

eg.
>>> list1=[' hello ', ' world ', ' How ', ' is ', ' you '
>>> list1.append ('! ')
>>> List1
[' Hello ', ' world ', ' How ', ' is ', ' you ', '! ']

List.insert (index, obj)
add element to specified position
Index: Indexed location
Obj: Content

eg.
>>> List1
[' Hello ', ' world ', ' How ', ' is ', ' you ', '! ']
>>> List1.insert (1, ', ')
>>> List1
[' Hello ', ', ', ' world ', ' How ', ' is ', ' you ', '! ']

List.extend (list_i)
Adds an element from the List_i list to the list

eg.
>>> List
[' Hello ', ' how ', ' is ', ' you ']
>>> list.extend ([' Good ', ' girl ')
>>> List
[' Hello ', ' how ', ' is ', ' you ', ' good ', ' girl ']

Delete

List.pop ():
Removes the element at the end of the list by default
List.pop (Index)
Deletes the element at the specified position, index

eg.
>>> List1
[' Hello ', ', ', ' world ', ' How ', ' is ', ' you ', '! ']
>>> List1.pop ()
‘!‘
>>> List1.pop (1)
‘,‘

Del List[index]
Deletes the element at the specified position, index
Del List
Delete entire list

eg.
>>> List1
[' Hello ', ' world ', ' How ', ' is ', ' you ']
>>> del List1[1]
>>> List1
[' Hello ', ' how ', ' is ', ' you ']

>>> List1
[' Hello ', ' how ', ' is ', ' you ']
>>> del List1
>>> List1
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' List1 ' is not defined

List.remove (obj)
Remove the first element of the list equal to obj

eg.
>>> list=[' hello ', ' world ', ' How ', ' is ', ' you '
>>> List.remove (' World ')
>>> List
[' Hello ', ' how ', ' is ', ' you ']

List.clear ()
Clear all the contents of the list

eg.
>>> list=[' hello ', ' world ', ' How ', ' is ', ' you '
>>> List.clear ()
>>> List
[]

Modify

List[index]=obj
Modifies the element at the specified position

eg.
>>> List1
[' Hello ', ' world ', ' How ', ' is ', ' you ']
>>> list1[0]= ' Hi '
>>> List1
[' Hi ', ' world ', ' How ', ' is ', ' you ']

Inquire

List[index]
Indexed by subscript, starting from 0

eg.
>>> list=[' hello ', ' world ', ' How ', ' is ', ' you '
>>> List[2]
' How '

LIST[A:B]
Slices, Gu Tou regardless of the tail

eg.
>>> list=[' hello ', ' world ', ' How ', ' is ', ' you '
>>> List[0:3]
[' Hello ', ' world ', ' how ']
>>> list[1:]
[' World ', ' How ', ' is ', ' you ']
>>> List[:3]
[' Hello ', ' world ', ' how ']
>>> list[:]
[' Hello ', ' world ', ' How ', ' is ', ' you ']

Meta-group

Operation

Meta-group

Method

Example

Increase

Tup=tup1+tup2
Tuples do not support modification, but can be increased by connecting combinations

eg.
>>> (tup1=)
>>> tup2= (4,5,6)
>>> tup=tup1+tup2
>>> Tup
(1, 2, 3, 4, 5, 6)

Delete

Del Tup
Tuples do not support single element deletions, but can delete entire tuples

eg.
>>> Tup
(1, 2, 3, 4, 5, 6)
>>> del Tup
>>> Tup
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' tup ' is not defined

Modify


TUP=TUP[INDEX1],TUP1[INDEX2], ...
TUP=TUP[INDEX1:INDEX2]
Tuples are immutable types and cannot modify elements of tuples. A new tuple can be constructed from existing string concatenation

eg.
>>> tup= (' A ', ' B ', ' C ', ' d ', ' e ')
>>> Tup=tup[1],tup[2],tup[4]
>>> Tup
(' B ', ' C ', ' e ')

>>> tup= (' A ', ' B ', ' C ', ' d ', ' e ')
>>> Tup=tup[1:3]
>>> Tup
(' B ', ' C ')

Inquire

Tup[index]
Indexed by subscript, starting from 0

eg.
>>> tup= (1,2,3,4)
>>> Tup[2]
3

TUP[A:B]
Slices, Gu Tou regardless of the tail

eg.
>>> tup= (1,2,3,4)
>>> Tup[1:3]
(2, 3)
>>> tup[1:]
(2, 3, 4)
>>> Tup[:3]
(1, 2, 3)
>>> tup[:]
(1, 2, 3, 4)

Dictionary

Operation

Dictionary

Method

Example

Increase

Dict[key]=value
Adding elements by assigning a value

eg.
>>> dict={' name ': ' Li ', ' Age ': 1}
>>> dict[' class ']= ' first '
>>> Dict
{' name ': ' Li ', ' age ': 1, ' class ': ' First '}

Dict.update (dict_i)
Update the key/value pairs of the new dictionary dict_i to the Dict (contains a different key from the dict for dict_i)

eg.
>>> dict={' name ': ' Li ', ' age ': 1, ' class ': ' First '}
>>> dict.update (school= ' Wawo ')
>>> Dict
{' name ': ' Li ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}

Delete

Del Dict[key]
Delete a single element, using key to specify the deletion
Del Dict
Delete Dictionary

eg.
>>> Dict
{' name ': ' Li ', ' age ': 1, ' class ': ' First '}
>>> del dict[' class ']
>>> Dict
{' name ': ' Li ', ' Age ': 1}

>>> dict1={' name ': ' Li ', ' age ': 1, ' class ': ' First '}
>>> del Dict1
>>> Dict1
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' Dict1 ' is not defined

Dict.pop (Key)
Delete a single element, using key to specify the deletion

eg.
>>> dict={' name ': ' Li ', ' age ': 1, ' class ': ' First '}
>>> dict.pop (' name ')
' Li '
>>> Dict
{' Age ': 1, ' class ': ' First '}

Dict.clear ()
Clear All content

eg.
>>> Dict
{' Age ': 1, ' class ': ' First '}
>>> Dict.clear ()
>>> Dict
{}

Modify

Dict[key]=value
Modifying a method by re-assigning a value to an existing key

eg.
>>> Dict
{' name ': ' Pang ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}
>>> dict[' name ']= ' Li '
>>> Dict
{' name ': ' Li ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}

Dict.update (dict_i)
Update the key/value pairs of the dictionary dict_i to the Dict (contains the same key as Dict for dict_i)

eg.
>>> Dict
{' name ': ' Li ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}
>>> dict.update (name= ' pang ')
>>> Dict
{' name ': ' Pang ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}

Inquire

Dict[key]
Accessing value values with key

eg.
>>> dict={' name ': ' Pang ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}
>>> dict[' name ']
' Pang '

Dict.items ()
Returns an array of traversed (key, value) tuples as a list

eg.
>>> dict={' name ': ' Pang ', ' age ': 1, ' class ': ' First ', ' School ': ' Wawo '}
>>> Dict.items ()
Dict_items ([' Name ', ' Pang '), (' Age ', 1), (' Class ', ' first '), (' School ', ' Wawo ')])

Dict.keys ()
Returns a dictionary of all key values in a list
Dict.values ()
Returns a dictionary of all values in a list

eg.
>>> Dict.keys ()
Dict_keys ([' Name ', ' Age ', ' class ', ' School ')
>>> dict.values ()
Dict_values ([' Pang ', 1, ' first ', ' Wawo ')

Dict.get (Key)
Returns the corresponding dictionary value for the specified key, without returning none

eg.
>>> dict.get (' age ')
1

Python sequence (list, tuple, dictionary) additions and deletions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.