Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Move given element to List Start
In Python, moving a specific element to the beginning of a list is a common operation when working with data structures. This article explores three different approaches to accomplish this task, each using different algorithms and techniques. We'll examine step-by-step procedures and provide complete Python implementations for each method.
What Does Moving an Element to List Start Mean?
Moving a given element to the beginning of a list involves rearranging the elements so that all instances of the specified element occupy the first positions. This operation is useful when you want to prioritize certain values or reorganize data for processing.
For example, if we have [4, 2, 6, 8, 2, 9] and want to move element 2 to the start, the result should be [2, 2, 4, 6, 8, 9].
Using a Temporary List
This approach creates an empty temporary list to collect all instances of the target element. We iterate through the original list, find matching elements, and build a new list with target elements first.
def move_to_start_using_temp_list(my_list, element):
target_elements = []
other_elements = []
for item in my_list:
if item == element:
target_elements.append(item)
else:
other_elements.append(item)
return target_elements + other_elements
numbers = [4, 2, 6, 8, 2, 9]
element = 2
result = move_to_start_using_temp_list(numbers, element)
print(result)
[2, 2, 4, 6, 8, 9]
Using List Comprehension
List comprehension provides a more concise way to achieve the same result. We create two separate lists ? one for matching elements and another for non-matching elements, then concatenate them.
def move_to_start_using_list_comprehension(my_list, element):
matching = [item for item in my_list if item == element]
non_matching = [item for item in my_list if item != element]
return matching + non_matching
numbers = [4, 2, 6, 8, 2, 9]
element = 2
result = move_to_start_using_list_comprehension(numbers, element)
print(result)
[2, 2, 4, 6, 8, 9]
Using Indexing and Pop
This approach modifies the original list in-place by finding each occurrence of the target element, removing it with pop(), and inserting it at the beginning with insert().
def move_to_start_using_indexing(my_list, element):
index = 0
insert_position = 0
while index < len(my_list):
if my_list[index] == element:
# Remove element from current position
removed_element = my_list.pop(index)
# Insert at the beginning (after previously moved elements)
my_list.insert(insert_position, removed_element)
insert_position += 1
else:
index += 1
return my_list
numbers = [4, 2, 6, 8, 2, 9]
element = 2
result = move_to_start_using_indexing(numbers.copy(), element)
print(result)
[2, 2, 4, 6, 8, 9]
Comparison
| Method | Time Complexity | Space Complexity | Modifies Original |
|---|---|---|---|
| Temporary List | O(n) | O(n) | No |
| List Comprehension | O(n) | O(n) | No |
| Indexing and Pop | O(n²) | O(1) | Yes |
Conclusion
The list comprehension approach offers the most readable and efficient solution with O(n) time complexity. Use the temporary list method when you need explicit control over the process, and the indexing approach when you must modify the original list in-place.
---