* operator and range() in python 3.x has many uses. One of them is to initialize the list.
Code : Intializing 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
- Move all zeroes to end of array using List Comprehension in Python
- Python List Comprehension to find pair with given sum from two arrays
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python List Comprehension | Three way partitioning of an array around a given range
- Python List Comprehension | Sort even-placed elements in increasing and odd-placed in decreasing order
- Python Dictionary Comprehension
- Ternary Operator in Python
- Operator Overloading in Python
- Difference between == and is operator in Python
- Operator Functions in Python | Set 2
- Operator Functions in Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.