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
      Reverse 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.

      Reverse Arrays

      In this chapter, we shall explore the different ways to rearrange the given array in the reverse order of the index. In Python, array is not one of the built-in data types. However, Python's standard library has array module. The array class helps us to create a homogenous collection of string, integer or float types.
      
      The syntax used for creating array is
      import array
      obj = array.array(typecode[, initializer])
      Let us first create an array consisting of a few objects of int type
      import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      The array class doesn't have any built-in method to reverse array. Hence, we have to use another array. An empty array "b" is declared as follows
      b = arr.array('i')
      Next, we traverse the numbers in array "a" in reverse order, and append each element to the "b" array −
      for i in range(len(a)-1, -1, -1):
      b.append(a[i])
      Te array "b" now holds numbers from original array in reverse order.

      Example 1

      Here is the complete code to reverse an array in Python
      import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = arr.array('i')
      for i in range(len(a)-1, -1, -1):
      b.append(a[i])
      print (a, b)
      It will produce the following output 
      array('i', [10, 5, 15, 4, 6, 20, 9]) array('i', [9, 20, 6, 4, 15, 5, 10])
      We can also reverse the sequence of numbers in an array using the reverse() method in list class. List is a built-in type in Python.
      We have to first transfer the contents of an array to a list with tolist() method of array class
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = a.tolist()
      We can call the reverse() method now
      b.reverse()
      If we now convert the list back to an array, we get the array with reversed order,
      a = arr.array('i')
      a.fromlist(b)

      Example 2

      Here is the complete code
      from array import array as arr
      a = arr.array('i', [10,5,15,4,6,20,9])
      b = a.tolist()
      b.reverse()
      a = arr.array('i')
      a.fromlist(b)
      print (a)
      It will produce the following output
      array('i', [10, 5, 15, 4, 6, 20, 9])

      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.