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.

W. Churchill
  • 346
  • 1
  • 7
  • 28
BrysonMG
  • 15
  • 5
  • 2
    Welcome to Stack Overflow! Please take the [tour](https://stackoverflow.com/tour), look around, and read through the [Help Center](https://stackoverflow.com/help), in particular [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) 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](https://stackoverflow.com/help/mcve). People will be glad to help – Andreas Jul 03 '19 at 02:20
  • Here is one way to iterate over the two lists simultaneously: https://stackoverflow.com/questions/21098350/python-iterate-over-two-lists-simultaneously?lq=1. 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. – jarmod Jul 03 '19 at 02:32

6 Answers6

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]
Austin
  • 25,759
  • 4
  • 25
  • 48
  • Comprehension expressions, `zip` and `isinntance` are too advanced for a complete beginner. Simple loops and an accumulator are more appropriate. – Xero Smith Jul 03 '19 at 03:19
  • @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`. – BrysonMG Jul 04 '19 at 04:16
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]
Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
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]
edilio
  • 1,778
  • 14
  • 13
  • 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. – edilio Jul 03 '19 at 02:53
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]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    This is not an obfuscation competition ;-) We're trying to teach basic Python to a beginner. – jarmod Jul 03 '19 at 02:47
-2
list3 = [x if not str(x).isalpha() else list2[i] for i, x in enumerate(list1)]

verified

MEOWWWWWWWW
  • 175
  • 2
  • 9
-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]
Lê Tư Thành
  • 1,063
  • 2
  • 10
  • 19