Fastest way to initialize a List in Python? (comparison of 3 options)

  Рет қаралды 53,365

Patrick Loeber

Patrick Loeber

Жыл бұрын

Fastest way to initialize a List in Python? (comparison of 3 options)
Get my Free NumPy Handbook:
www.python-engineer.com/numpy...
⭐ Join Our Discord : / discord
📓 ML Notebooks available on Patreon:
/ patrickloeber
If you enjoyed this video, please subscribe to the channel:
▶️ : / @patloeber
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-engineer.com
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-engineer.com/newsl...
📸 Instagram - / patloeber
🦾 Discord: / discord
▶️ Subscribe: / @patloeber
~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
🅿 Patreon - / patrickloeber
#Python

Пікірлер: 26
@GN69420
@GN69420 Жыл бұрын
Using [[0] * n] * n] creates a list of a list at the same point in memory, i.e. myList[0] is myList[1]
@kimtgf7152
@kimtgf7152 Жыл бұрын
Same with strings, I struggled so much while doing a programming question because of this
@MaxMustermann-on2gd
@MaxMustermann-on2gd Жыл бұрын
@@kimtgf7152 can expand on this? I wonder what the problem with strings is.
@kimtgf7152
@kimtgf7152 Жыл бұрын
@@MaxMustermann-on2gd Well, I had a n queens problem, and I had to make the board with each row being a string and all those strings were stored in a list. If I used something like ["...."]*n then what would happen is, if I changed one string all the other strings got changed as well.
@Eknoma
@Eknoma Жыл бұрын
​@@kimtgf7152That shouldn't happen... how are you changing your strings? Python strings are immutable, so unless you changed the memory directly, that should be impossible
@kimtgf7152
@kimtgf7152 Жыл бұрын
@@Eknoma So I just went back and saw my code, I think I was just using strings in lists in list, because of that error was appearing, it had nothing to do with the strings but the lists of strings in list. I was later using join to convert it each row of list of strings into a string.
@calum.macleod
@calum.macleod Жыл бұрын
I love how Python optimises some things, in ways that we might not have expected. I really like your channel, I've only been programming Python for a year but you are helping to set important ideas in my head that were not obvious before. Thank you.
@patloeber
@patloeber Жыл бұрын
Happy to hear that!
@pattmehta5835
@pattmehta5835 Жыл бұрын
+1 for the disclaimer to not use this technique in a nested list. Very useful for scenarios where latency is critical
@alexpaciniat
@alexpaciniat Жыл бұрын
Mutables can create tricky situations, whether as multiplicative elements in lists, where it is effectively the same object in memory and repeated, or when used as default argument in a function, where if modified it will effectively change the default value of the next call to that function.
@GyroCannon
@GyroCannon Жыл бұрын
The optimization is (assuming that the underlying structure is an array list) probably due to the code knowing how big to make the list. The other ways probably default to 10 or something and double in size each time it gets too big
@shivanshmathur1287
@shivanshmathur1287 7 ай бұрын
#Fastest as it uses numpy, which is a low level language import numpy as np n = 5 # Replace 5 with your desired size Listofzeros = np.zeros(n) MyList = list(Listofzeros) print(MyList)
@pzkpfw917
@pzkpfw917 7 ай бұрын
Sir, Loeber!! Thank you for the content on your YT channel.
@ytusr-ko5rg
@ytusr-ko5rg 10 ай бұрын
I've recognized it when creating a 2d list: board1 = [[0]*3]*3 # would be different from board2 = [[0 for j in range(3)] for i in range(3)] # let's take a closer look # when I want to update the cell data of the board[row][column] >>> board1[0][0] = 1 >>> board1 [[1,0,0],[1,0,0],[1,0,0]] >>> board2[0][0] = 1 >>> board2 [[1,0,0],[0,0,0],[0,0,0]] # it's dangerous. Be careful with this python behavior:) # Using list-comprehension is the correct way.
@bekhzodravshanov2161
@bekhzodravshanov2161 Жыл бұрын
You cant multiply nested list. You have to use loop for creating it
@MohammadBahiraei
@MohammadBahiraei Жыл бұрын
Wonderful
@amrosamier8547
@amrosamier8547 Жыл бұрын
Thank you..what IDE are you using please
@devnarula6733
@devnarula6733 Жыл бұрын
looks like vscode to me
@syukcode
@syukcode 11 ай бұрын
How to create Pointers in python?
@gsravan7058
@gsravan7058 7 ай бұрын
Hi
@MrJonas1995
@MrJonas1995 Жыл бұрын
Why do you write the "_" in "for _ in list"? You usually use something like "for element in list", don't you?
@MatrixAbuz17
@MatrixAbuz17 Жыл бұрын
The ‘_’ in for loops indicates to the programmer that the temporary variable will be ignored.
@Dekxterr
@Dekxterr 11 ай бұрын
what ide is this?
@matinsasan
@matinsasan 19 күн бұрын
Vs code
@Dekxterr
@Dekxterr 19 күн бұрын
@@matinsasan Thankyou and cool pfp man
6 Tips to write BETTER For Loops in Python
9:19
Patrick Loeber
Рет қаралды 246 М.
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1,1 МЛН
터키아이스크림🇹🇷🍦Turkish ice cream #funny #shorts
00:26
Byungari 병아리언니
Рет қаралды 25 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 842 М.
Modern Python logging
21:32
mCoding
Рет қаралды 155 М.
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 367 М.
Probability Comparison: Phobias and Fears
11:00
Reigarw Comparisons
Рет қаралды 11 МЛН
Unlocking your CPU cores in Python (multiprocessing)
12:16
mCoding
Рет қаралды 294 М.
Functools is one of the MOST USEFUL Python modules
13:37
Carberra
Рет қаралды 31 М.
Python dataclasses will save you HOURS, also featuring attrs
8:50
If __name__ == "__main__" for Python Developers
8:47
Python Simplified
Рет қаралды 382 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 795 М.
Diagnose slow Python code. (Feat. async/await)
9:57
mCoding
Рет қаралды 209 М.