0

I'm working with a couple of lists that have unicode values in them. I'm comparing values in one list to see if those values exist in the second list. Then I'm trying to take the matching values from the second list and put them in a third list. When I try to print the third list, it returns 'None'. What am I missing? Feedback is greatly appreciated!

# -*- coding: utf-8 -*-
import os

def GetFilepaths(directory):
    file_paths = []
    for root, directories, files in os.walk(directory):
        for filename in files:
            filepath = os.path.join(root, filename)
            file_paths.append(filepath)
    return file_paths

umlauts = [u'Ä', u'Ü', u'Ö', u'ä', u'ö', u'ü']
filePathsList = GetFilepaths(u'C:\\Scripts\\Replace Characters\\Umlauts')
filesWithUmlauts = []
for files in filePathsList:
    for umlaut in umlauts:
        if umlaut in files:
            filesUmlautPaths = filepathsUmlauts.append(files)
print filesUmlautPaths

1 Answer 1

1

This is because the return type of append is None

Example:

>>> filepathsUmlauts = []
>>> filepathsUmlauts.append(1)
>>> filepathsUmlauts
[1]
>>> x = filepathsUmlauts.append(2)
>>> x
>>> print x
None
>>> filepathsUmlauts
[1, 2]
>>> 

Just replace:

filesUmlautPaths = filepathsUmlauts.append(files)

with

filepathsUmlauts.append(files)

Another optimization in your code (which i would let you figure out by yourself) would be to use set.intersection - which would eliminate the nested for loops.

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

1 Comment

set.intersection actually won't work, by the way -- intersect won't check for substrings.

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.