Offset List Elements

PythonPythonBeginner
Practice Now

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

Introduction

In Python, lists are a commonly used data structure. Sometimes, you may need to move certain elements of a list to the end of the list. In this challenge, you will write a function that takes a list and an offset as arguments and returns a new list with the specified amount of elements moved to the end of the list.


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/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") subgraph Lab Skills python/comments -.-> lab-13701{{"`Offset List Elements`"}} python/lists -.-> lab-13701{{"`Offset List Elements`"}} python/tuples -.-> lab-13701{{"`Offset List Elements`"}} python/function_definition -.-> lab-13701{{"`Offset List Elements`"}} end

Offset List Elements

Write a function offset(lst, offset) that takes a list lst and an integer offset as arguments and returns a new list with the specified amount of elements moved to the end of the list. If the offset is positive, move the first offset elements to the end of the list. If the offset is negative, move the last offset elements to the beginning of the list.

def offset(lst, offset):
  return lst[offset:] + lst[:offset]
offset([1, 2, 3, 4, 5], 2) ## [3, 4, 5, 1, 2]
offset([1, 2, 3, 4, 5], -2) ## [4, 5, 1, 2, 3]

Summary

In this challenge, you wrote a function that takes a list and an offset as arguments and returns a new list with the specified amount of elements moved to the end of the list. You used slice notation to get the two slices of the list and combine them before returning.

Other Python Tutorials you may like