* operator and range() in python 3.x has many uses. One of them is to initialize the list.
Code : Initializing 1D-list list in Python
list1 : [0, 0, 0, 0, 0] list2 : [0, 0, 0, 0, 0]
Here, Only difference is star operator is used outside of the list. And range() is used inside. These two can also be used with a list within the list or Multidimensional list.
Code : list within list using * operation and range()
list1 : [, , , , ] list2 : [, , , , ]
The real glitch is with the multidimensional list. While dealing with a multidimensional list, initialization method matters a lot. Both methods * operator and list comprehension behaves differently.
Code : Multi-dimensional List
Star list : [8, [0, 8], [0, 8], [0, 8], [0, 8]] Range list : [8, , [0, 8], , ]
If someone wants to deal with 1D-array, one can use anything. But with the multidimensional array, one should use list comprehension.
- Python List Comprehension | Segregate 0's and 1's in an array list
- Python List Comprehension and Slicing
- Count set bits using Python List comprehension
- Python List Comprehension | Three way partitioning of an array around a given range
- Python List Comprehension to find pair with given sum from two arrays
- Move all zeroes to end of array using List Comprehension in Python
- List comprehension and ord() in Python to remove all characters other than alphabets
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python Dictionary Comprehension
- Operator Functions in Python | Set 2
- Ternary Operator in Python
- Python | Operator.countOf
- Operator Overloading in Python
- Difference between == and is operator in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh