Example of the list Initialization Method in Python: pythonlist
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'] >>> 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.