Swap first and last element in list python

Given a list, write a Python program to swap first and last element of the list.

Examples: 

Input : [12, 35, 9, 56, 24]
Output : [24, 35, 9, 56, 12]

Input : [1, 2, 3]
Output : [3, 2, 1]

Approach #1: Find the length of the list and simply swap the first element with [n-1]th element.

Python3

def swapList[newList]:

    size = len[newList]

    temp = newList[0]

    newList[0] = newList[size - 1]

    newList[size - 1] = temp

    return newList

newList = [12, 35, 9, 56, 24]

print[swapList[newList]]

Output:

[24, 35, 9, 56, 12]

Approach #2: The last element of the list can be referred as list[-1]. Therefore, we can simply swap list[0] with list[-1].

Python3

def swapList[newList]:

    newList[0], newList[-1] = newList[-1], newList[0]

    return newList

newList = [12, 35, 9, 56, 24]

print[swapList[newList]]

Output:

[24, 35, 9, 56, 12]

Approach #3: Swap the first and last element is using tuple variable. Store the first and last element as a pair in a tuple variable, say get, and unpack those elements with first and last element in that list. Now, the First and last values in that list are swapped. 

Python3

def swapList[list]:

    get = list[-1], list[0]

    list[0], list[-1] = get

    return list

newList = [12, 35, 9, 56, 24]

print[swapList[newList]]

Output:

[24, 35, 9, 56, 12]

Approach #4: Using * operand. 
This operand proposes a change to iterable unpacking syntax, allowing to specify a “catch-all” name which will be assigned a list of all items not assigned to a “regular” name. 

Python3

list = [1, 2, 3, 4]

a, *b, c = list

print[a]

print[b]

print[c]

Output: 

1
[2, 3]
4

Now let’s see the implementation of above approach: 

Python3

def swapList[list]:

    start, *middle, end = list

    list = [end, *middle, start]

    return list

newList = [12, 35, 9, 56, 24]

print[swapList[newList]]

Output:

[24, 35, 9, 56, 12]

Approach #5: Swap the first and last elements is to use the inbuilt function list.pop[]. Pop the first element and store it in a variable. Similarly, pop the last element and store it in another variable. Now insert the two popped element at each other’s original position. 

Python3

def swapList[list]:

    first = list.pop[0]  

    last = list.pop[-1]

    list.insert[0, last] 

    list.append[first]  

    return list

newList = [12, 35, 9, 56, 24]

print[swapList[newList]]

Output: 

[24, 35, 9, 56, 12]

How do you interchange the first and last element of a list in Python?

Approach #5: Swap the first and last elements is to use the inbuilt function list. pop[]. Pop the first element and store it in a variable. Similarly, pop the last element and store it in another variable.

How do you swap items in a list in Python?

To swap two list elements x and y by value, get the index of their first occurrences using the list. index[x] and list. index[y] methods and assign the result to variables i and j , respectively. Then apply the multiple assignment expression lst[i], lst[j] = lst[j], lst[i] to swap the elements.

How do you change the last element of a list in Python?

Use array1. pop[] instead. Then you can just do: array1 += array2 ..
That doesn't address the "replace the last element" aspect. ... .
You're right. ... .
itertools. ... .
Indeed but depending on the use case it's OK as it's as iterable as a list..

How do you access the first element of a list in Python?

Lists work similarly to strings -- use the len[] function and square brackets [ ] to access data, with the first element at index 0. [See the official python.org list docs.]

Chủ Đề