1

I have a list containing a number of ints and adding them to a second list at the index matching their value. The second list has already bin filled with placeholders ("X"). i.e:

firstlist = [2, 3, 5]

with the output:

secondlist = ['X', 'X', 2, 3, 'X', 5, 'X', 'X']

What is the best way to do so? Any help greatly appreciated!

4
  • 1
    What is second list contain before the output? In your example before the change does secondlist contain 8 X's or only 5 X's? Commented Jan 3, 2019 at 18:41
  • 1
    what did you try and what is the specific problem you have? Commented Jan 3, 2019 at 18:41
  • @GeeTransit the secondlist before being changed to the current output is just a list filled with 10 "X"s as placeholders. Commented Jan 3, 2019 at 18:42
  • @user7755 does that mean that the correct solution here would overwrite three Xs and delete one? I'm confused. Commented Jan 3, 2019 at 19:01

4 Answers 4

1

This will take a list argument and put them into a new list to be returned by the function.

def list_fill(first_list, fill, length=None):
    return [(i if i in first_list else fill) for i in (range(max(first_list) + 1) if length is None else range(length))]

How list_fill works:

The first_list argument includes the indexes to be changed.
The fill argument is what the empty spaces should be.
The third optional length argument specifies the length of the output. If left blank, it will take the max index specified in first_list.

Sign up to request clarification or add additional context in comments.

5 Comments

This creates a new list, which seems to be at odds with what the OP is looking for. In fact it doesn't use the second list at all shrug
I've edited it so it can have a length argument. If the output list was desired to be length 5, you can use list_fill((1, 3, 4), 'X', 5). The secondlist the OP had is just filled with 'X'. You can put that in the fill argument.
Sure, I don't disagree that this might be a superior method, but it does not appear to do what OP is wanting to do. It's possible that's just a matter of unclear specification though (that's never been a problem in programming :D)
I think the OP's secondlist serves no real purpose, just an output list created beforehand. Anyhow, functions can be used anywhere, so incase the OP is using this everywhere in their code, it can minimize code length :D
Never actually thought about filling a list with an if-else statement, but that actually does seem like an excellent way to add placeholders!
0

Are you looking for something as simple as this:

firstlist = [2, 3, 5]
secondlist = ['X']*10

for index in firstlist:
    secondlist[index] = index

print(secondlist)
# ['X', 'X', 2, 3, 'X', 5, 'X', 'X', 'X', 'X']

Could have a dict for firstlist with indices for key and the value you want to place as values for the dict.

1 Comment

Actually yes! Never thought it was that simply. Thanks!
0

You could use .insert() if you want to keep the stuff that's already in secondlist.

firstlist = [2, 3, 5]
secondlist = ['X', 'X', 'X', 'X', 'X']

for x in range(len(firstlist)):
    secondlist.insert(firstlist[x], firstlist[x])

print(secondlist)

Comments

0

Let's say you need to make a function YourFunc(inputlist,length) where inputlist argument is the list for input and length is the argument for the length of the list for output. So, according to your question, inputlist=[2,3,5] and length=8.

def YourFunc(inputlist,length):
        out=[]
        for X in range (0,length):
                if inputlist.count(X)==1:
                         out.append(X)
                elif inputlist.count(X)==0:
                         out.append('X')
        print(out)

YourFunc([2,3,5],8)

So, this is how I usually do. In this function a loop is running from 0to length. If the is element is not in inputlist then 'X' is inserted in output list out. If not, then the number in the loop is inserted.

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.