Created
May 5, 2022 20:55
-
-
Save aaronshaver/7563fa830f77344d3b61ebab66fc0fd4 to your computer and use it in GitHub Desktop.
Python list.append() vs list.extend()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# append adds its argument as a single element to the end of a list. | |
# The length of the list itself will increase by one. | |
# extend iterates over its argument adding each element to the list, extending the list | |
my_list = [1, 2, 3] | |
my_list.append([4, 5, 6]) | |
# [1, 2, 3, [4, 5, 6]] | |
my_list = [1, 2, 3] | |
my_list.extend(4, 5, 6]) | |
# [1, 2, 3, 4, 5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment