Python list pop left

Shift Values in a List Using Python

In Python, the easiest way to shift values in a list is with the Python list pop(), insert(), and append() functions.

list = [0,1,2,3] #shifted backwards (to left) list.append(list.pop(0)) print(list) list = [0,1,2,3] #shifted forward (to right) list.insert(0,list.pop()) print(list) #Output: [1,2,3,0] [3,0,1,2]

You can also use the deque() data structure from the Python collections module to shift a list.

from collections import deque items = deque([0,1,2,3]) #shifted backwards (to left) items.rotate(-1) print(items) items = deque([0,1,2,3]) #shifted forward (to right) items.rotate(1) print(items) #Output: deque([1,2,3,0]) deque([3,0,1,2])

You can also use list slicing to shift a list forward or backwards in Python.

list = [0,1,2,3] list_shifted_backwards = list[1:] + list[:1] list_shifted_forward = list[-1:] + list[:-1] print(list_shifted_backwards) print(list_shifted_forward) #Output: [1,2,3,0] [3,0,1,2]

In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to change the order of the items of a list in an easy way.

With Python, we can easily shift the items in a list both to the right or the left.

To shift items to the left, we can remove the first element from the list with pop(), and then append it to the end of the list with the append() function.

To shift items to the right, we can do the opposite. Shifting to the right involves removing the last element from the list, and then prepending it to the beginning of the list.

Читайте также:  Accessing attributes in javascript

Below is an example in Python of how to shift values in a list using the pop(), append(), and insert() functions.

list = [0,1,2,3] #shifted backwards (to left) list.append(list.pop(0)) print(list) list = [0,1,2,3] #shifted forward (to right) list.insert(0,list.pop()) print(list) #Output: [1,2,3,0] [3,0,1,2]

If you need to shift a list multiple times, you can use a loop and apply these operations as many times as needed.

Below is a function which will shift values in a list multiple times to the left or right depending on the argument values passed.

def shiftList(list,direction,n): if direction == "backwards": for i in range(0,n): list.append(list.pop(0)) else: for i in range(0,n): list.insert(0,list.pop()) return list print(shiftList([0,1,2,3,4,5,6],"backwards",2)) print(shiftList([0,1,2,3,4,5,6],"forwards",3)) #Output: [2, 3, 4, 5, 6, 0, 1] [4, 5, 6, 0, 1, 2, 3]

Using deque in Python to Shift a List

Another way that you can shift a list is with the deque data structure from the Python collections module.

Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster than the similar list operations.

To shift the elements of a list, we can convert it to a deque object and then use the rotate() function.

Below are some examples of how to shift items in a list with the deque rotate() function.

from collections import deque items = deque([0,1,2,3]) #shifted backwards (to left) items.rotate(-1) print(items) items = deque([0,1,2,3]) #shifted forward (to right) items.rotate(1) print(items) #Output: deque([1,2,3,0]) deque([3,0,1,2])

If you want to shift the items multiple times, you just pass the number of times to rotate().

from collections import deque items = deque([0,1,2,3,4,5,6]) #shifted backwards (to left) items.rotate(-3) print(items) items = deque([0,1,2,3,4,5,6]) #shifted forward (to right) items.rotate(2) print(items) #Output: deque([3, 4, 5, 6, 0, 1, 2]) deque([5, 6, 0, 1, 2, 3, 4])

Shifting a List in Python with Slicing

You can also shift the items in a list in Python using list slicing.

To shift a list backwards, we slice the list from the second element to the end, and then add a slice with only the first element to the end of first slice.

To shift a list forward, we slice the list from the second to last element to the beginning, and then add a slice with only the last element to the beginning of first slice.

Below is an example of how to shift a list both backwards and forward with list slicing using Python.

list = [0,1,2,3] list_shifted_backwards = list[1:] + list[:1] list_shifted_forward = list[-1:] + list[:-1] print(list_shifted_backwards) print(list_shifted_forward) #Output: [1,2,3,0] [3,0,1,2]

If you need to shift a list multiple times, we can define a function which shifts the list a specified number of items.

Below is a function which will shift the items in a list using slicing multiple times to the left or right depending on the argument values passed.

def shiftList(list,direction,n): if direction == "backwards": new_list = list[n:] + list[:n] else: new_list = list[-n:] + list[:-n] return new_list print(shiftList([0,1,2,3,4,5,6],"backwards",2)) print(shiftList([0,1,2,3,4,5,6],"forwards",3)) #Output: [2, 3, 4, 5, 6, 0, 1] [4, 5, 6, 0, 1, 2, 3]

Hopefully this article has been useful for you to learn how to shift lists in Python.

  • 1. How to Slice a Dictionary in Python
  • 2. Check if Variable is Float in Python
  • 3. Find All Pythagorean Triples in a Range using Python
  • 4. Using readlines() and strip() to Remove Spaces and \n from File in Python
  • 5. Using Python to Sum Even Numbers in List
  • 6. Python Check if Object Has Attribute
  • 7. Remove Trailing Zeros from String with rstrip() in Python
  • 8. Write Variable to File Using Python
  • 9. Skip Numbers in Python Range
  • 10. pandas cumsum – Find Cumulative Sum of Series or DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Источник

Оцените статью