Example of the list initialization method in Python

Source: Internet
Author: User
This article mainly introduces the list initialization method in Python, analyzes the commonly used list initialization methods and related precautions, for more information about list initialization in Python, see the following article.

This example describes the list initialization method in Python. We will share this with you for your reference. The details are as follows:

1. Basic method.

lst = [1, 2, 3, 4, 5]

2. initialize continuous numbers.

>>> lst = [n for n in range(5, 10)]>>> print(lst)[5, 6, 7, 8, 9]

3. initialize n identical values. (Two methods)

>>> lst = ['x' for n in range(5)]>>> print(lst)['x', 'x', 'x', 'x', 'x']>>> lst = ['z']*5>>> print(lst)['z', 'z', 'z', 'z', 'z']>>> lst = [0]*3>>> print(lst)[0, 0, 0]

4. the dictionary, set, list, and tuples of Python are represented by curly brackets, brackets, and parentheses respectively. For example:

Dictionary: dic = {'a': 12, 'B': 34} set: s = {1, 2, 3, 4} List: li = [1, 2, 3, 3] tuples: tup = (1, 2, 3, 4) # tuples are unchangeable lists.


For more articles about the list initialization method example in Python, refer to PHP Chinese network!

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.