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!