Python – Move given element to List Start


In Python, working with records may be a visit errand, and one common operation is moving a particular component to the beginning of a list. This article digs into the subject by examining multiple approaches that employ different algorithms to realize this objective. We are going altogether investigate the step-by-step procedures involved in each approach and go with them with the comparing Python sentence structure. By doing so, we point to supply a comprehensive understanding of the strategies and enable users to successfully control records in Python. So, let's jump into the investigation of these approaches, unraveling the complexities of moving a given component to the start of a list utilizing different calculations and picking up important experiences in the Python code required to actualize each procedure. 

Move given element to List Start

Moving a given component to the beginning of a list includes improving the components so that the required component possesses the primary position. This operation is commonly performed when controlling records in Python. By moving the component to the beginning, able to adjust the list's structure and prioritize particular esteem.

To realize this, we can use diverse approaches. One approach includes making a transitory list, repeating through the first list, and adding the given component to the brief list when it is experienced. After emphasizing the whole list, we evacuate the component from its unique position and amplify the list with the brief list. This successfully moves the component to the beginning. 

  • Approach 1 − Using a Temporary List

  • Approach 2 − Using List Comprehension

  • Approach 3 − Using Indexing and Pop

Approach 1:  Using a Temporary List

In this approach, we make an empty list. We emphasize the first list and check in case the current component matches the given component. In case a coordinate is found, we add the component to the empty list. After the emphasis, we evacuate the element from the initial list. At long last, we amplify the first list with the brief list and return the overhauled list. This approach viably moves the given component to the beginning of the list.

Example

def move_to_start_using_temp_list(my_list, element):
    temp_list = []
    for item in my_list:
        if item == element:
            temp_list.append(item)
            my_list.remove(element)
    my_list = temp_list + my_list
    return my_list
my_list = [4, 2, 6, 8, 2, 9]
element = 2
print(move_to_start_using_temp_list(my_list, element))

Output

[2, 2, 4, 6, 8, 9]

Approach 2:  Using List Comprehension

With list comprehension, we make a new list. We include the given component to the unused list and after that add all components from the first list that are not rise to the given component. By combining the component at the beginning and the sifted components from the first list, we get the specified result. This approach disentangles the code and accomplishes the assignment in a brief way.

Example

def move_to_start_using_list_comprehension(my_list, element):
    return [item for item in my_list if item == element] + [item for item in my_list if item != element]

my_list = [4, 2, 6, 8, 2, 9]
element = 2
print(move_to_start_using_list_comprehension(my_list, element))

Output

[2, 2, 4, 6, 8, 9]

Approach 3:  Using Indexing and Pop

In this approach, we discover the record of the given component within the list utilizing the list() strategy. At that point, we evacuate the component from the list utilizing the pop() strategy with the found record. After evacuating the component, we embed it at the beginning of the list utilizing ordering. This approach straightforwardly alters the first list and moves the component to the starting. It offers a clear arrangement to the issue. 

Example

def move_to_start_using_indexing(my_list, element):
    index = 0
    while index < len(my_list):
        if my_list[index] == element:
            my_list.pop(index)
            my_list.insert(0, element)
            index += 1
        else:
            index += 1
    return my_list

my_list = [4, 2, 6, 8, 2, 9]
element = 2
print(move_to_start_using_indexing(my_list, element))

Output

[2, 2, 4, 6, 8, 9]

Conclusion

In this article, we investigated three distinctive approaches to moving a given component to the beginning of a list in Python. We talked about the calculations included and gave a comparison of Python sentence structure for each approach. By understanding these procedures, you'll be able effectively control records in your Python programs. Keep in mind, the approach you select may depend on the particular necessities of your application. It's fundamental to consider variables such as time complexity, memory utilization, and the measure of the list when choosing which approach to actualize. By mastering list manipulation procedures in Python, you will be way better prepared to illuminate a wide extent of programming issues productively 

Updated on: 01-Sep-2023

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements