Skip to content

Instantly share code, notes, and snippets.

@aaronshaver
Created May 5, 2022 20:55
Show Gist options
  • Save aaronshaver/7563fa830f77344d3b61ebab66fc0fd4 to your computer and use it in GitHub Desktop.
Save aaronshaver/7563fa830f77344d3b61ebab66fc0fd4 to your computer and use it in GitHub Desktop.
Python list.append() vs list.extend()
# 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