w3resource logo


Python Exercises

Python Challenges: Push the first number to the end of a list

Python Challenges - 1: Exercise-28 with Solution

Write a Python program to the push the first number to the end of a list.

Input : [0,2,3,4,6,7,10]
Output : [2, 3, 4, 6, 7, 10, 0]

Sample Solution :-

Python Code :

def move_last(num_list):
    a = [num_list[0] for i in range(num_list.count(num_list[0]))]
    x = [ i for i in num_list if i != num_list[0]]
    x.extend(a)
    return(x)
l1 = [0,2,3,4,6,7,10]
l2 = [10,0,11,12,14,0,17]
print("Original List: ",l1)
print(move_last(l1))
print("Original List: ",l2)
print(move_last(l2))

Output :

Original List:  [0, 2, 3, 4, 6, 7, 10]                                  
[2, 3, 4, 6, 7, 10, 0]                                                  
Original List:  [10, 0, 11, 12, 14, 0, 17]                              
[0, 11, 12, 14, 0, 17, 10]

Flowchart:

Python Flowchart: Push first number to the end of a list

Python Code Editor:

def move_last(num_list):
    a = [num_list[0] for i in range(num_list.count(num_list[0]))]
    x = [ i for i in num_list if i != num_list[0]]
    x.extend(a)
    return(x)
l1 = [0,2,3,4,6,7,10]
l2 = [10,0,11,12,14,0,17]
print("Original List: ",l1)
print(move_last(l1))
print("Original List: ",l2)
print(move_last(l2))

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program