Home > database >  How to remove something from one list and add it to another in python
How to remove something from one list and add it to another in python

Time:11-15

I have a nested list that has certain elements with an [x] at the beginning. I want the function to remove those elements and move them to the last list in list1 (at index 2). But it should remove the [x] from it before placing it in the last list. It should also count how many were removed from each list.

For example:

list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']

# After:

list1 = [['stretch'], ['school'], ['sleep','midterm', 'homework', 'eat', 'final']]

# Output:
# 2 removed from 1st list
# 1 removed from 2nd list

CodePudding user response:

list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
new_list = [list() for i in range(len(list1))]
new_list[2] = list1[2]

items = 0
for i in range(len(list1)-1):
    for j in list1[i]:
        if "[x]" in j:
            new_list[2].append(j.replace("[x]", ""))
            items  = 1
        else:
            new_list[i].append(j)

print(list1, new_list, items)

CodePudding user response:

I think you have a data structure problem. Your code shouldn't match user input, you should get away from that as soon as possible, and separate display and storage. That being said, you can take a step towards those changes by still discarding the silliness asap:

Example of running:

$ python code.py
[['[x]homework', '[x]eat', 'stretch'], ['[x]final', 'school'], ['sleep', 'midterm']]
changed count is 0
[['stretch'], ['school'], ['sleep', 'midterm', 'homework', 'eat', 'final']]

with source code like below

def main():
    list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
    print(list1)
    count, list2 = process(list1)
    print(f'changed count is {count}')
    print(list2)
         
def parse(silly_item):
    if silly_item.startswith('[x]'):
        finished = True
        raw_item = silly_item[3:]
    else:
        finished = False
        raw_item = silly_item
    return finished, raw_item

def process(silly_data_format):
    todos, done = silly_data_format[:-1], silly_data_format[-1]
    count = 0
    new_todos = []
    new_done = done[:]
    for todo in todos:
        new_todo = []
        for silly_item in todo:
            finished, raw_item = parse(silly_item)
            target = new_done if finished else new_todo
            target.append(raw_item)
        new_todos.append(new_todo)
    new_silly_data_format = new_todos   [new_done]
    return count, new_silly_data_format

if __name__ == '__main__':
    main()

CodePudding user response:

This is how you would do that:

list1 = [['[x]homework', '[x]eat','stretch'], ['[x]final', 'school'], ['sleep','midterm']]
for x in range(0,len(list1)-1):
    lst = list1[x]
    count =  0
    to_be_removed = []
    for str in lst:
        if str[0:3] == "[x]":
            to_be_removed.append(str)
            list1[-1].append(str[3:])
            count  = 1
    for str in to_be_removed:
        lst.remove(str)
    print(f"{count} items were removed from list {x 1}" )
print(list1)
  • Related