Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
communitycreator

What is the array fromlist() function Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

An array is used to store multiple items, elements, or values of the same datatype in a single variable.

The fromlist() function is simply used to append or add a list to the ending of a given array.

Syntax

array.fromlist(list)
Syntax

Parameter value

The fromlist() method takes a list as the parameter value.

Return value

The fromlist() method returns an array having a list added to the end to the array.

Example

# importing the array module
import array as arr
# creating an integer data type array
x = arr.array('i', [1,2,3,4,5,6,7])
# creatomg a list
y = [8, 9, 10]
# using the fromlist() function
x.fromlist(y)
# Printing the new array
print(x)
Implementing the fromlist() method

Explanation

  • Line 2: We import the array module in python.
  • Line 5: We create an array variable x.
  • Line 9: We create a string variable y.
  • Line 12: We use the fromlist() function to add the list in y to the array x.
  • Line 15: We print the new array x which now has the list values in y added to its ending.

RELATED TAGS

python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring