From ca355ce75ca394dabbab4c4bbc5f1996cd3a3011 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 20 Aug 2019 15:21:41 +0300 Subject: [PATCH] Add initialize_list_with_range snippet --- snippets/initialize_list_with_range.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/initialize_list_with_range.md diff --git a/snippets/initialize_list_with_range.md b/snippets/initialize_list_with_range.md new file mode 100644 index 000000000..a2faf2ba3 --- /dev/null +++ b/snippets/initialize_list_with_range.md @@ -0,0 +1,21 @@ +--- +title: initialize_list_with_range +tags: list,beginner +--- + +Initializes a list containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`. + +Use list comprehension and `range()` to generate a list of the appropriate length, filled with the desired values in the given range. +Omit `start` to use the default value of `0`. +Omit `step` to use the default value of `1`. + +```py +def initialize_list_with_range(end, start = 0, step = 1): + return [x for x in range(start, end + 1, step)] +``` + +```py +initialize_list_with_range(5) # [0, 1, 2, 3, 4, 5] +initialize_list_with_range(7,3) # [3, 4, 5, 6, 7] +initialize_list_with_range(9,0,2) # [0, 2, 4, 6, 8] +```