w3resource

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.

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))

Sample 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:

Contribute your code and comments through Disqus.

Previous: Write a Python program to push all zeros to the end of a list.
Next: Write a Python program to find majority element in a list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/challenges/1/python-challenges-1-exercise-28.php