If by "array" you actually mean a Python list, you can use

a = [0] * 10

or

a = [None] * 10
Answer from Sven Marnach on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.4 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
🌐
Python Guides
pythonguides.com › create-an-empty-array-in-python
Create An Empty Array In Python
December 23, 2024 - To create an empty array in Python using lists, simply initialize an empty list with square brackets.
🌐
Finxter
blog.finxter.com › how-to-return-an-empty-list-from-a-function-in-python
How to Return an Empty List from a Function in Python? – Be on the Right Side of Change
You can return an empty new list from a Python function using the square bracket notation [] after the return keyword using the expression return [].
🌐
w3resource
w3resource.com › numpy › array-creation › empty.php
NumPy: numpy.empty() function - w3resource
NumPy array creation: numpy.empty() function, example - Return a new array of given shape and type, without initializing entries.
🌐
Reddit
reddit.com › r/learnpython › should i return none for an empty data array?
Should I return None for an empty data array? : r/learnpython
June 23, 2024 - Subreddit for posting questions and asking for general advice about all topics related to learning python. ... Sorry, this post was deleted by the person who originally posted it. Share ... Agreed. This is a special case of the Null Object Pattern. It simplifies what callers need to be prepared to handle. For example, if a caller wants to iterate over all elements, they don't have to check for None first, they can just iterate over the empty list. ... If it's an empty list return an empty list.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy empty array with examples
NumPy Empty Array With Examples - Spark By {Examples}
March 27, 2024 - This function takes three arguments, ... syntax and how to use the numpy.empty() function which returns an array of uninitialized data of the given shape, order, and datatype....
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.5.dev0 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.1 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 9213731 › returning-and-empty-array
python - Returning and empty array - Stack Overflow
There's no return and this isn't a function definition. ... Well the situation in which a empty list shouldn't happen. But neigthe should what you expect, if what you said in your comment is true.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.2 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
🌐
TutorialsPoint
tutorialspoint.com › how-to-initialize-an-empty-array-of-given-length-using-python
How to Initialize an Empty Array of given Length using Python
In Python, we have some built?in functions such as empty(), append(), range(), and, extend() will be used to Initialize an empty array of the given length. ... The empty() is an in?built function in Python that returns the new array in the form of given length and type.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.empty.html
numpy.empty — NumPy v2.3 Manual
Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None. ... Return an empty array with shape and type of input.
Top answer
1 of 5
4

There are multiple problems with your code.

The first one is that you iterate over the key-value pairs in the dictionary, and later try to use the values as keys here: if trs[k] == i: and here translated.append(trs[v]) These should be just k and v instead of trs[k] and trs[v].

The second problem is a bigger one: after fixing the previous, the code still gives wrong answer. The words are in random order. This is because you iterate over the dictionary items in the outer loop instead of the words themselves. This can easily be fixed by changing the order of the loops.

The third is that I think the function should return a string. Just return " ".join(translated) at the end.

And the fourth is that you actually don't use the dictionary as a dictionary. You use it as a list, but it's not how they are meant to be used. dicts are direct mappings of values, you don't need to iterate over all the entries all the time. Use the in and [] operators.

So here is how this code should look like:

def translate(a):
    trs = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"}
    translated = []
    for i in a.split(" "): 
      if i in trs:
        translated.append(trs[i])
    return " ".join(translated)

print translate("merry christmas and happy new year")
# prints "god jul och gott nytt ar"
2 of 5
3
def translate(a):
    trs = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"ar"}
    translated = []
    for i in a.split(" "):
        for k, v in trs.iteritems():
            if i == k:
                translated.append(trs[i])
    return translated

print translate("merry christmas and happy new year")

# output:
# ['god', 'jul', 'och', 'gott', 'nytt', 'ar']
🌐
Programiz
programiz.com › python-programming › numpy › methods › empty
NumPy empty()
... # create an array of uninitialized ... or arbitrary data that remained in the current memory location. ... The empty() method returns the array of given shape, order, and datatype filled with arbitrary data....
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › empty
Python Numpy empty() - Create Empty Array | Vultr Docs
November 18, 2024 - The numpy.empty() function in Python is a part of the NumPy library, commonly used for generating arrays with uninitialized entries. This method proves useful primarily when you need to allocate an array quickly without immediately populating ...
🌐
Quora
quora.com › How-do-I-create-an-empty-array-in-Python
How to create an empty array in Python - Quora
Answer (1 of 7): The closest thing to an array in Python is a list, which is dynamic (the size can change). This is somewhat similar to a C++ [code ]std::vector[/code] or a Java [code ]ArrayList[/code] (if you’re familiar with those languages and data structures).
🌐
Quora
quora.com › How-do-I-check-if-an-array-is-empty-in-Python
How to check if an array is empty in Python - Quora
Answer (1 of 12): I am going to assume you are talking about lists (Python does have arrays, but they are very different to lists). Three ways : 1 Test the truthiness If you know the item is a list you do : [code]if not my_list: print(‘List is empty’) [/code]Empty containers (lists,sets,t...