1

I am working on my very first coding project, which is to make a text encoder. Once complete, I plan to make the decoder that will pair with it. For now, I am having trouble getting two lists to combine/overlap. I apologize if what I am about to show has an actual name that references it, I am new to coding and still learning many things.

list1 = [20.0, 'X', 'X', 46.0, 0.0, 18.0, 'X', 40.0]
list2 = ['Y', 31.0, 45.0, 'Y', 'Y', 'Y', 47.0, 'Y']

I need the output to be:

list3 = [20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0]

Both lists have an equal number of values, and I need to combine them into one list, keep the numbers in their current order, and eliminate the "X"s and "Y"s entirely.

2
  • 2
    Welcome to Stack Overflow! Please take the tour, look around, and read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help Commented Jul 3, 2019 at 2:20
  • Here is one way to iterate over the two lists simultaneously: stackoverflow.com/questions/21098350/…. You can use isintance(s, str) to test if s is a string. You can use list3.append(x) to append x to list3. Combine those and you will have a solution. Commented Jul 3, 2019 at 2:32

6 Answers 6

2

Use zip with isinstance in a list-comprehension:

list1 = [20.0, 'X', 'X', 46.0, 0.0, 18.0, 'X', 40.0]
list2 = ['Y', 31.0, 45.0, 'Y', 'Y', 'Y', 47.0, 'Y']

list3 = [x if isinstance(x, float) else y for x, y in zip(list1, list2)]
# [20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0]
Sign up to request clarification or add additional context in comments.

2 Comments

Comprehension expressions, zip and isinntance are too advanced for a complete beginner. Simple loops and an accumulator are more appropriate.
@Xero I have a little bit of familiarity with zip, even though I am still learning all of the different ways to implement it. but this is the first I am hearing of isinstance.
1
list1 = [20.0, 'X', 'X', 46.0, 0.0, 18.0, 'X', 40.0]
list2 = ['Y', 31.0, 45.0, 'Y', 'Y', 'Y', 47.0, 'Y']

list3 = []

for x in range(len(list1)):
    if isinstance(list1[x], float):
        list3.append(list1[x])

    else:
        list3.append(list2[x])

print(list3)

OUTPUT:-

[20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0]

3 Comments

@jarmod can you please tell me why?
Your answer works, but is unpythonic. See python-forum.io/…
@jarmod Thanks for your suggestion. I would look forward to do as you mentioned in the later future.
1

As @Austin said use zip to combine several lists of the same size.

Here is a version a little easier to understand if your new to development

def get_number(item1, item2):
    if item1 in ['X', 'Y']:
        return item2
    else:
        return item1

[get_number(x, y) for x, y in zip(list1, list2)]

OUTPUT:-

[20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0]

1 Comment

True we shouldn't be hardcoding values but in this case the problem is not clear. The problem says we need to eliminate Xs and Ys, it doesn't really states that it should be float values. So what happens if is really an int for example.
0

Why not max with isinstance:

print([max(i, key=lambda x: isinstance(x, float)) for i in zip(list1, list2)])

Output:

[20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0]

1 Comment

This is not an obfuscation competition ;-) We're trying to teach basic Python to a beginner.
-2
list3 = [x if not str(x).isalpha() else list2[i] for i, x in enumerate(list1)]

verified

3 Comments

Er, no. Did you test this?
Your code results in the list [0, 1, 2, 3, 4, 5, 6, 7].
sorry for previous answer... I edited code and it works
-2

This is a suggestion if you don't seriously care about the order of comnbined list

list1 = [20.0, 'X', 'X', 46.0, 0.0, 18.0, 'X', 40.0]
list2 = ['Y', 31.0, 45.0, 'Y', 'Y', 'Y', 47.0, 'Y']
a = list1 + list2
a = [x for x in a if not isinstance(x, str)]
>>>a
[20.0, 46.0, 0.0, 18.0, 40.0, 31.0, 45.0, 47.0]

4 Comments

It does not output in the expected form.
@Austin, you mean the order of list?
That's not what the OP asked for. The output should be [20.0, 31.0, 45.0, 46.0, 0.0, 18.0, 47.0, 40.0].
@Austin, thanks for reminding. I edited my answer as a suggestion.

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.