Python for loop to manipulate list and remove elements once used

SDROB

I have a list of lists where I need to move the second and third element to be put in to the first element (which is a list) in first and last place respectively. I then need to remove the elements once used.

I already have a for loop which will work for the first instance, but I am unsure how to safely remove these elements and move to the next set and repeat operation. I understand for loops are not good for removing elements.

Example list

x = ['string 1', 1, 2, 'string 2', 0, 'string 3'], 'new string 1', 'new string 2', ['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4', ['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6', ['string 9', 19, 2, 'string 10', 0, 'string 11']

where new string 1 should be put in front of string 1 and new string 2 is appended to the list, then all first 10 values (technically the first 3 elements as the first is a list) are removed.

The following for loop achieves this for one instance

def sorting_insert_list(x):

        x = list(x)
        for i in range(len(x)):
                keyword = x[1]
                location = x[2]
        x[0].append(location)
        x[0].insert(0, keyword)

        print(x)

and returns the following results

[['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3', 'new string 2'], 'new string 1', 'new string 2', ['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4', ['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6']

The final list I require would look like the following:

sorted_list = ['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3','new string 2',]   ['new string 3''string 3', 19, 2, 'string 4', 0, 'string 5', 'new string 4',], ['new string 5','string 6', 1, 2, 'string 7', 0, 'string 8','new string 6']

It may be worth noting I will be doing this for a huge list, I have just simplified the data to show as an example. Thanks.

Adding attempt to incorporate date, which appears then in the list after new string 2 and in the position x[3] :

def sorting_insert_list(x):
    result = []
    for i in range(0, len(3)):
        keyword = x[i + 1]
        location = x[i + 2]
        date = insert_values[i + 3]
        result += [[keyword] + x[i] + [location] + date]
    x = result
    print(x)
recnac

I think the best way to do this is abandon original list x, and create a new list to compose your result, instead of removing 2nd, 3nd elements from list, you can try this:

# list comprehension, more pythonic
def sorting_insert_list(x):
    x = [[x[i+1]] + x[i] + [x[i+2]] for i in range(0, len(x) // 3 * 3, 3)]
    print(x)

# base on your version
def sorting_insert_list(x):
    result = []
    for i in range(0, len(x) // 3 * 3, 3):
        keyword = x[i + 1]
        location = x[i + 2]
        result += [[keyword] + x[i] + [location]]
    x = result
    print(x)

test:

x = [['string 1', 1, 2, 'string 2', 0, 'string 3'], 'new string 1', 'new string 2',
     ['string 3', 19, 2, 'string 4', 0, 'string 5'], 'new string 3', 'new string 4',
     ['string 6', 1, 2, 'string 7', 0, 'string 8'], 'new string 5', 'new string 6',
     ['string 9', 19, 2, 'string 10', 0, 'string 11']]

sorting_insert_list(x)

output:

[['new string 1', 'string 1', 1, 2, 'string 2', 0, 'string 3', 'new string 2'], ['new string 3', 'string 3', 19, 2, 'string 4', 0, 'string 5', 'new string 4'], ['new string 5', 'string 6', 1, 2, 'string 7', 0, 'string 8', 'new string 6']]

by the way, len(x) // 3 * 3 is used to ignore the tail which is less than 3, like your example. If you assure len(x) % 3 == 0, you can just use len(x)

Hope that helps you, and comment if you have further questions. : )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove list elements in a for loop in Python?

How to remove list elements in a for loop in Python?

How to remove list elements within a loop effectively in python

Python remove list elements

Is it possible to loop all the elements of the list at once

How can the getter/setter method be used in python to manipulate a list?

For loop only iterates once when trying to remove classes from elements

Remove elements from python list

loop through array list to remove specified elements

Python for loop to select elements in list

For loop in python to check elements in list

How to remove all elements in a list that appear more than once

Remove elements from a list based on a condition in Python, problem: loop removes only the first instance

Loop to gradually to remove elements to dictionary in Python

Using a list with string elements be used as the name of the files to create in a for loop

Is there a way to manipulate the counter in a "for" loop in python

Remove elements from nested list - Python

Remove element, then iterate and combine elements in a list python

How to remove duplicates elements in the list in Python

Pandas Python remove elements from list with condition

Remove elements from python list based on filter

Remove multiple elements from a list of index with Python

Remove many elements from a list python

Python remove only alphabet elements from a list

Remove strings from elements of a list - Python

Python remove elements from two dimensional list

Remove specific elements from list Python

Using raw input to remove elements of a list in python

Iterate and remove HTML from list elements in python