How to Return an Array in Python?


In this blog post, we will be discussing how to return an array in Python. We will be exploring a few different methods on how to do this. As always, feel free to leave any comments or questions below.

Method 1: Append to an empty list

In this method, we will create an empty list and then append the elements of our array one by one.

#python def return_array(MyArr): new_array = [] #create an empty list for i in range(len(MyArr)): new_array.append(MyArr[i]) #append each element of the array to the new list return new_array #return the new list
Code language: Python (python)

Method 2: Use the built-in function “list”

In this method, we will use the built-in function “list” to convert our array into a list. This is a shorter method but it only works if our array is made up of elements that can be converted into a list data type. For example, if our array contained strings then this method would work but if our array contained dictionaries then this method would not work.

def return_array(arr): return list(arr) #use the built-in function "list" to convert arr into a list and return it
Code language: Python (python)

Method 3: Use NumPy’s “array” function

If we are working with NumPy arrays then we can use NumPy’s “array” function to convert our array into a regular Python array. Again, this is a shorter method but it only works if we are working with NumPy arrays.

#python import numpy as np #import NumPy library def return_array(arr): return np.array(arr).tolist() #use NumPy's "array" function to convert arr into a regular Python array and return it
Code language: Python (python)

You need to use a dictionary instead of a list if your want to use key-value pairs. furthermore, you need to return the values outside of for the loop. You can try the following code.

def GetInput(): names = {} # Needs to declare your dict percentages = {} # Needs to declare your dict for counter in range(0, 3): names[counter] = input("Please enter the student's name: ") valid = False while valid == False: percentages[counter] = int(input("Please enter the student's score %: ")) if percentages[counter] < 0 or percentages[counter] > 100: print("Please enter a valid % [0-100]") else: valid = True return names, percentages # Return outside of for loop. name, mark = GetInput() print(name) print(mark)
Code language: Python (python)

Output

>>> python3 test.py Please enter the student's name: bob Please enter the student's score %: 20 Please enter the student's name: ann Please enter the student's score %: 30 Please enter the student's name: joe Please enter the student's score %: 40 {0: 'bob', 1: 'ann', 2: 'joe'} {0: 20, 1: 30, 2: 40}
Code language: PHP (php)

If you want to create a common dictionary which contains the students’ name and percentages, you can try the following implementation:

def GetInput(): students = {} for _ in range(0, 3): student_name = input("Please enter the student's name: ") valid = False while not valid: student_percentages = int(input("Please enter the student's score %: ")) if student_percentages < 0 or student_percentages > 100: print("Please enter a valid % [0-100]") continue valid = True students[student_name] = student_percentages return students students = GetInput() print(students)
Code language: Python (python)

Output

>>> python3 test.py Please enter the student's name: ann Please enter the student's score %: 20 Please enter the student's name: bob Please enter the student's score %: 30 Please enter the student's name: joe Please enter the student's score %: 40 {'ann': 20, 'bob': 30, 'joe': 40}
Code language: PHP (php)

Read more here.

In this blog post, we discussed how to return an array in Python. We explored three different methods on how to do this including using an empty list, using the built-in “list” function, and using NumPy’s “array” function. As always, feel free to leave any comments or questions below!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts