Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Join Arrays

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Join Arrays

      In Python, array is a homogenous collection of Python's built in data types such as strings, integer or float objects. However, array itself is not a built-in type, instead we need to use the array class in Python's built-in array module.

      First Method

      To join two arrays, we can do it by appending each item from one array to other.
      Here are two Python arrays
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i', [2,7,8,11,3,10])
      Run a for loop on the array "b". Fetch each number from "b" and append it to array "a" with the following loop statement
      for i in range(len(b)):
      a.append(b[i])
      The array "a" now contains elements from "a" as well as "b".
      Here is the complete code
      import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i', [2,7,8,11,3,10])
      for i in range(len(b)):
      a.append(b[i])
      print (a, b)
      It will produce the following output
      array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])

      Second Method

      Using another method to join two arrays, first convert arrays to list objects
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i', [2,7,8,11,3,10])
      x=a.tolist()
      y=b.tolist()
      The list objects can be concatenated with the '+' operator.
      z=x+y
      If "z" list is converted back to array, you get an array that represents the joined arrays
      a.fromlist(z)
      Here is the complete code
      from array import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i', [2,7,8,11,3,10])
      x=a.tolist()
      y=b.tolist()
      z=x+y
      a=arr.array('i')
      a.fromlist(z)
      print (a)

      Third Method

      We can also use the extend() method from the List class to append elements from one list to another.
      First, convert the array to a list and then call the extend() method to merge the two lists
      from array import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i', [2,7,8,11,3,10])
      a.extend(b)
      print (a)
      It will produce the following output
      array('i', [10, 5, 15, 4, 6, 20, 9, 2, 7, 8, 11, 3, 10])

      Practice with Online Editor

      Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
      Remember to save code(Ctrl + S Or Command + S) before run it.