0

I need to be able to add an item into list a from list b. The item from list b is to be added as soon as ' ' which is a double space is identified.

Therefore if the first item in the list is not a double space, then the loop goes on to check the next item in the list, if its also not a double space, it carries on until if finds the double space and then it replaces the first available double space with the item from list b. This should be looped so that if I run the function again, an item in list b is popped and added to the next available double space in list a.

a = ['a','c','e','j','h','  ','  ','  ','  ']
b = ['b','d','f','i','g']

x = 4
for item in a:
    if item == a[4]:
        break
if a[x] != '  ':
    a[x+1] = b.pop(-2)

else:
    a[x] = a[x+1]

print("list a: ",a)
print("List b: ",b)

Output:

list a:  ['a', 'c', 'e', 'j', 'h', 'i', '  ', '  ', '  ']
List b:  ['b', 'd', 'f', 'g']

That works, but I have a feeling my code doesn't work on all inputs. Does it? If it doesn't, what's wrong?

4
  • So it works or not? x=4 is i and i really moved from b to the first available place in list a Commented Mar 9, 2012 at 20:21
  • @OfirBaruch; i was kinda cheating to get it to work but i think x should be = 0 to enable it to start checking from the first item in list a. but i could not get it to work with x=0. Commented Mar 9, 2012 at 20:25
  • so am i understanding this correctly? you need to replace each double space in a with an element from b, in order? Commented Mar 9, 2012 at 20:29
  • @asia1281; thats right, but only an item at a time. Commented Mar 9, 2012 at 20:40

5 Answers 5

2

I think this is what you are looking for:

def move_item(a, b):
    a[a.index('  ')] = b.pop()

>>> a = ['a','c','e','j','h','  ','  ','  ','  ']
>>> b = ['b','d','f','i','g']
>>> move_item(a, b)
>>> print('list a: ', a, '\nlist b: ', b)
list a:  ['a', 'c', 'e', 'j', 'h', 'g', '  ', '  ', '  ']
list b:  ['b', 'd', 'f', 'i']
>>> move_item(a, b)
>>> print('list a: ', a, '\nlist b: ', b)
list a:  ['a', 'c', 'e', 'j', 'h', 'g', 'i', '  ', '  ']
list b:  ['b', 'd', 'f']
Sign up to request clarification or add additional context in comments.

2 Comments

but i only need one item at a time from list b. how could i edit your code to achieve this.
@Lycon - I've edited my answer to make it easier to do this one item at a time.
2

This:

a = [b.pop() if item == '  ' else item for item in a]

gets you:

['a', 'c', 'e', 'j', 'h', 'g', 'i', 'f', 'd']

Take a look at Python list comprehensions

Comments

0

You didn't ask a question, but here are some hints:

list.index, list.pop, a[index_of_doublespace] = popped_value_from_b

Comments

0

Since this homework, I'm just going to give you some hints:

  1. You'll need to work with a indexes:

    for i in range(len(a)):
        if a[i] == ??? :
            a[i] = ??
    
  2. Why are you popping the -2th element? Check out what pop does.

Comments

0

I guess you want to do this:

[x.split(' ')[0] or b.pop() for x in a]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.