Python create list and add element method to list

Source: Internet
Author: User

Today's learning content is related to the list in Python.

I. Create a list

1. Create a normal list

>>> Tabulation1 = [' The Holy Man ', ' canopy ', ' roll the curtain ']>>> tabulation1[' st ', ' canopy ', ' roller blinds ']
>>> Tabulation2 = [72,36,18]>>> tabulation2[72, 36, 18]

2. Create a mixed list

>>> Mix tabulation = [' The Holy Man ', 72, ' canopy ', 36]syntaxerror:invalid syntax>>> mixtabulation = [' The sage ', 72, ' canopy ', 36 ]>>> mixtabulation[' Holy Sage ', 72, ' canopy ', 36]

3. Create an empty list

>>> empty = []>>> empty[]

There are three ways to introduce you, and next, what do you do if you want to add elements to the list?

Two. Adding elements to the list

1.append

>>> tabulation1.append (' Purple Chardonnay ')
>>> Tabulation1
[' The Holy Man ', ' canopy ', ' roller blinds ', ' violet Chardonnay ']

2.extend

>>> tabulation1.extend ([' Purple Chardonnay ', ' Green Xia '])
>>> Tabulation1
[' The Holy Man ', ' canopy ', ' roller blinds ', ' Purple Chardonnay ', ' Purple Chardonnay ', ' Green Xia ']

There is a way to expand the list with extend, and it is important to note that this method uses a list to expand the list instead of adding elements directly, so add "[]" to "()".

3.insert

>>> Tabulation1.insert (1, ' Purple Chardonnay ') >>> tabulation1[' Holy sage ', ' Purple Chardonnay ', ' canopy ', ' roller blinds ', ' Purple Chardonnay ', ' Purple Chardonnay ', ' green Chardonnay '

Do not want to eat in situ and so die, so I choose to progress a little bit every day.

Python create list and add element method to 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.