Skip to content

Instantly share code, notes, and snippets.

@jlollis
Created March 22, 2019 04:56
Show Gist options
  • Save jlollis/2374a56e1e39120d7f4be77c4e802ec8 to your computer and use it in GitHub Desktop.
Save jlollis/2374a56e1e39120d7f4be77c4e802ec8 to your computer and use it in GitHub Desktop.
Python list basics
# Create an empty list using square brackets.
numbers = []
print(numbers) # Output: []
# Create an empty list using list().
numbers = list()
print(numbers) # Output: []
# Create a list of numbers.
numbers = [1, 2, 3]
print(numbers) # Output: [1, 2, 3]
# Create a list of numbers in a range.
numbers = list(range(1, 4))
print(numbers) # Output: [1, 2, 3]
# Append to a list of numbers.
numbers = [1, 2]
print(numbers) # Output: [1, 2]
numbers.append(3)
print(numbers) # Output: [1, 2, 3]
# Create a list of tuples.
tuples_list = [(1, 2), (2, 4), (3, 6)]
print(tuples_list) # Output: [(1, 2), (2, 4), (3, 6)]
# Create a list of lists.
list_of_lists = [[1, 2], [2, 4], [3, 6]]
print(list_of_lists) # Output: [[1, 2], [2, 4], [3, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment