Python basics-list

Source: Internet
Author: User
1. create new_list1 [TV, Car, Cloth, Food] new_list2list ([TV, Car, Cloth, Food]) print (new_list1) print (new_list2) in list: [TV, car, Cloth, Food] [TV, Car... 1. create a list

New_list1 = ['TV', 'car', 'body', 'food']
New_list2 = list (['TV', 'car', 'body', 'food'])
Print (new_list1)
Print (new_list2)
Running result:
['TV', 'car', 'cloth ', 'food']
['TV', 'car', 'cloth ', 'food']

2. list common methods:

1. append

New_list1 = ['TV', 'car', 'body', 'food']
Print (new_list1)
New_list1.append ('water') # append 'water'
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food']
['TV', 'car', 'cloth ', 'food', 'water']

2. count

New_list1 = ['TV', 'car', 'body', 'food', 'food']
Print (new_list1.count ('food') # count the number of times 'food' is in the list => 2

3. extend

New_list1 = ['TV', 'car', 'body', 'food', 'food']
New_list1.extend ([11,22, 33, 'cart'])
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food', 'food', 11, 22, 33, 'car']

4. sort

New_list1 = ['TV', 'car', 'body', 'food', 'food']
Print (new_list1)
New_list1.sort () # forward sorting
Print (new_list1)
New_list1.sort (reverse = True) # reverse sorting
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food', 'food']
['Car', 'br', 'food', 'food', 'TV'] # positive sorting result
['TV', 'food', 'food', 'body', 'car'] # reverse sorting result

5. len

New_list1 = ['TV', 'car', 'food', 'body', 'food']
Print (len (new_list1) => 5 # Number of print elements

6. remove

New_list1 = ['TV', 'car', 'food', 'body', 'food']

New_list1.remove ('food ')
Print (new_list1)
Running result:
['TV', 'car', 'cloth ', 'food'] # remove deletes the first identical element found.

7. pop

New_list1 = ['TV', 'car', 'food', 'body', 'food']
New_list1.pop () # Delete an element randomly
Print (new_list1)
New_list1.pop (2) # specify to delete an element
Print (new_list1)
The running result is:
['TV', 'car', 'food', 'bry']
['TV', 'car', 'body'] #. The first element 'food' is deleted here'

8. index

New_list1 = ['TV', 'car', 'food', 'body', 'food']
Print (new_list1.index ('food') # return the index value of the specified element. if the same element is used, the first one is returned.
Running Result: 2

9. insert

New_list1 = ['TV', 'car', 'food', 'body', 'food']
New_list1.insert (2, 'hahahaha') # insert an element to a specified position
Print (new_list1)
The running result is:
['TV', 'car', 'hahaha', 'food', 'body', 'food']

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.