What you are using is called a list comprehension in Python, not an inline for-loop (even though it is similar to one). You would write your loop as a list comprehension like so:
p = [q.index(v) if v in q else 99999 for v in vm]
When using a list comprehension, you do not call list.append
because the list is being constructed from the comprehension itself. Each item in the list will be what is returned by the expression on the left of the for
keyword, which in this case is q.index(v) if v in q else 99999
. Incidentially, if you do use list.append
inside a comprehension, then you will get a list of None
values because that is what the append
method always returns
Related Posts:
- What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
- IndentationError: unexpected indent error
- Putting a simple if-then-else statement on one line [duplicate]
- How can I do a line break (line continuation)?
- if/else in a list comprehension
- syntaxerror: “unexpected character after line continuation character in python” math
- `from … import` vs `import .` [duplicate]
- if/else in a list comprehension
- if else in a list comprehension
- What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
- if else in a list comprehension
- How to find all occurrences of an element in a list
- Python: SyntaxError: keyword can’t be an expression
- List comprehension on a nested list?
- How to get the union of two lists using list comprehension?
- How to normalize a 2-dimensional numpy array in python less verbose?
- SyntaxError invalid token
- What is the difference between ‘/’ and ‘//’ when used for division?
- Creating a dictionary from a csv file?
- Printing variables in Python 3.4
- Why is there no tuple comprehension in Python?
- Why is there no tuple comprehension in Python?
- Error “‘type’ object has no attribute ‘__getitem__'” when iterating over list[“a”,”b”,”c”]
- Is it possible to use ‘else’ in a list comprehension?
- Python def marked as invalid syntax
- bash: syntax error near unexpected token `(‘ – Python
- TypeError: only size-1 arrays can be converted to Python scalars (matplotlib)
- Why do I get TypeError: can’t multiply sequence by non-int of type ‘float’?
- Writing a pandas DataFrame to CSV file
- When should I use uuid.uuid1() vs. uuid.uuid4() in python?
- ImportError: No module named tensorflow
- Best way to convert string to bytes in Python 3?
- How do you change the size of figures drawn with Matplotlib?
- Count unique values per groups with Pandas
- Get unique values from a list in python [duplicate]
- How do you decode Base64 data in Python?
- Convert pandas Series to DataFrame
- TypeError: ‘type’ object is not subscriptable when indexing in to a dictionary
- Error: all the input array dimensions except for the concatenation axis must match exactly
- Expected 2D array, got 1D array instead error
- syntaxerror: “unexpected character after line continuation character in python” math
- How to prevent errno 32 broken pipe?
- The equivalent of a GOTO in python [duplicate]
- tkinter gui with progress bar
- Invalid character in identifier
- What does “\r” do in the following script?
- Convert list to tuple in Python
- Python: TypeError: object of type ‘NoneType’ has no len()
- anaconda – path environment variable in windows
- Pandas: ValueError: cannot convert float NaN to integer
- Get key by value in dictionary
- How to add multiple values to a dictionary key in python?
- Where can I find a list of the Flask SQLAlchemy Column types and options?
- How can I install Python’s pip3 on my Mac?
- convert json ipython notebook(.ipynb) to .py file
- how to update spyder on anaconda
- Django upgrading to 1.9 error “AppRegistryNotReady: Apps aren’t loaded yet.”
- Pygame Drawing a Rectangle
- Purpose of `numpy.log1p( )`?
- Removing Punctuation From Python List Items
- TypeError(“‘bool’ object is not iterable”,) when trying to return a Boolean
- “OverflowError: Python int too large to convert to C long” on windows but not mac
- Create a Pandas Dataframe by appending one row at a time
- ‘Pip’ is Not Recognized as an Internal or External Command windows 10 CMD
- Failed to load the native TensorFlow runtime – TensorFlow 2.1
- Using headers with the Python requests library’s get method
- How to truncate float values?
- How do I write a “tab” in Python?
- TypeError: ‘builtin_function_or_method’ object has no attribute ‘__getitem__’
- How to use pygame.KEYDOWN to execute something every time through a loop while the key is held down?
- How to create an integer array in Python?
- Recursion in Python? RuntimeError: maximum recursion depth exceeded while calling a Python object
- Writing a dictionary to a csv file with one line for every ‘key: value’
- How to remove square brackets from list in Python?
- Errno 10061 : No connection could be made because the target machine actively refused it ( client – server )
- Python Error – TypeError: input expected at most 1 arguments, got 3 [duplicate]
- A general tree implementation?
- TypeError: expected string or buffer
- How to convert a file into a dictionary?
- Getting the name of a variable as a string
- can’t multiply sequence by non-int of type ‘list’
- Read data (.dat file) with Pandas
- Is there any numpy group by function?
- ‘instancemethod’ object has no attribute ‘__getitem__’ with class variables
- Python/Scikit-Learn – Can’t handle mix of multiclass and continuous
- Sorting list of lists by the first element of each sub-list
- Install py2exe for python 2.7 over pip: this package requires Python 3.3 or later
- What is the correct format to write float value to file in Python
- How is Python’s List Implemented?
- ImportError: No module named IPython
- How to find length of digits in an integer?
- What exactly do “u” and “r” string flags do, and what are raw string literals?
- “E: Unable to locate package python3-pip”
- How to search for a string in text files?
- TypeError: ‘zip’ object is not subscriptable
- Pip: could not find a version. No matching distribution found
- Compare two columns using pandas
- ValueError: total size of new array must be unchanged
- Shuffle an array with python, randomize array item order with python
- No handlers could be found for logger