0

I'm trying to use Python 3 to use list_one to sort list_two and output to list_sorted. If the value of a field is missing then I'd like to see a null value placed. The output would have the same number of items as list_one

list_one = ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
list_two = ['seven', 'five', 'four', 'three', 'one']
list_sorted = []

for i in list_one:
    for field in list_two:
        if i == field:
            list_sorted.append(field)
        else:
            list_sorted.append(None)

print ('final output:', list_sorted)

The desired output is:

['one', None, 'three', 'four', 'five', None, 'seven']

But it's actually outputting:

[None, None, None, None, 'one', None, None, None, None, None, None, None, None, 'three', None, None, None, 'four', None, None, None, 'five', None, None, None, None, None, None, None, None, 'seven', None, None, None, None]

My current thinking is the answer involves enumerate but I'm not sure. Any help would be greatly appreciated.

1
  • Doesn't really look like you're sorting anything. Commented Oct 18, 2017 at 0:50

2 Answers 2

1

Convert list_two into a set, and then build a list comprehension based on whether that element is in list_two or not.

set_two = set(list_two)
list_sorted = [x if x in set_two else None for x in list_one] 

print(list_sorted)
['one', None, 'three', 'four', 'five', None, 'seven']
Sign up to request clarification or add additional context in comments.

2 Comments

That's awesome! Thanks @coldspeed. I have some reading to do. I don't know if this is appropriate for StackOverflow but I have two more questions: Is that what they call 'ternary' or shorthand? And if so, what is the longhand version? I don't understand what's happening there. Second question is what if list_two is two dimensional?
@Jarvis No, this is called a "List comprehension". A ternary or shorthand is a different concept involving cascaded if-else statements in a single line (you don't see that here). As to your second question, the solution would be similar, you just have to extend the dimensions by 1. If you want to know how to do that, please ask a new question. Furthermore, you can mark the most helpful answer accepted, so please consider doing that. Thanks.
1

You're not really sorting anything, but it looks like you could achieve what you wanted just testing whether i is present in list_two. Remove the inner for loop.

list_one = ['one', 'two', 'three', 'four', 'five', 'six', 'seven']
list_two = ['seven', 'five', 'four', 'three', 'one']
list_sorted = []

for i in list_one:
    if i in list_two:
        list_sorted.append(i)
    else:
        list_sorted.append(None)

print ('final output:', list_sorted)

2 Comments

Better convert to a set for quick lookup! stackoverflow.com/a/46801255/4909087
The important bit is x if x in set_two else None which facilitates constant lookup, since sets can support lookup in O(1), while lists (such as list_two) cannot. The difference cannot be seen in small lists, but this method is quadratic in complexity. Still, nice effort, so you have my up vote :)

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.