Initialize List with Range

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, you can create a list of numbers using the range() function. However, sometimes you may want to create a list of numbers that are not sequential or that start at a different number than 0. In such cases, you can use the initialize_list_with_range() function.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13661{{"`Initialize List with Range`"}} python/variables_data_types -.-> lab-13661{{"`Initialize List with Range`"}} python/lists -.-> lab-13661{{"`Initialize List with Range`"}} python/tuples -.-> lab-13661{{"`Initialize List with Range`"}} python/function_definition -.-> lab-13661{{"`Initialize List with Range`"}} python/default_arguments -.-> lab-13661{{"`Initialize List with Range`"}} python/data_collections -.-> lab-13661{{"`Initialize List with Range`"}} python/build_in_functions -.-> lab-13661{{"`Initialize List with Range`"}} end

Initialize List with Range

Write a function initialize_list_with_range(end, start=0, step=1) that initializes a list containing the numbers in the specified range where start and end are inclusive with their common difference step.

The function should return a list of the appropriate length, filled with the desired values in the given range.

Input

  • end (integer) - The end of the range (inclusive).
  • start (integer, optional) - The start of the range (inclusive). Default is 0.
  • step (integer, optional) - The common difference between each number in the range. Default is 1.

Output

  • A list containing the numbers in the specified range.
def initialize_list_with_range(end, start = 0, step = 1):
  return list(range(start, end + 1, step))
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]

Summary

In this challenge, you learned how to create a list of numbers using the initialize_list_with_range() function. This function allows you to create a list of numbers that are not sequential or that start at a different number than 0.

Other Python Tutorials you may like