Python--list

Source: Internet
Author: User

1, define the way:

li=[element 1, Element 2,.....]

Li=list () Create an empty list by default

2. Convert an iterative element to a list

Li=list (an iterative element) inside for a for loop each element as a list of elements

A, string conversion to List

Str= "Yang Ruijing" li=list (str) print (LI)
result: [' Yang ', ' Rui ',    ' Jing ']

B, Ganso convert to List
tu= ("Yang","Rui","jing") Li=list (tu) print (LI)

C, dictionary conversion to list

Dictionary conversion list has three forms of key value items because the dictionary loop is a loop key by default

(1) Default key conversion list

      

dict={"K1":"Yang","K2":"Rui","K3":"Jing"}li=list (dict) print (LI) ['K3','K1','K2']

(2) Value conversion list

    

dict={"K1":"Yang","K2":"Rui","K3":"Jing"}li=list (dict.values ()) print (LI) ['Yang','Rui','Jing']

(3) Items conversion list

    

dict={"K1":"Yang","K2":"Rui","K3":"Jing"}li=list (Dict.items ()) print (LI) [('K1','Yang'), ('K3','Jing'), ('K2','Rui')]

Python--list

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.