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 Tuples

      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 Tuples

      In Python, a Tuple is classified as a sequence type object. It is a collection of items, which may be of different data types, with each item having a positional index starting with 0. Although this definition also applies to a list, there are two major differences in list and tuple. First, while items are placed in square brackets in case of List (example: [10,20,30,40]), the tuple is formed by putting the items in parentheses (example: (10,20,30,40)).
      In Python, a Tuple is an immutable object. Hence, it is not possible to modify the contents of a tuple one it is formed in the memory.
      However, you can use different ways to join two Python tuples.

      Example 1

      All the sequence type objects support concatenation operator, with which two lists can be joined.
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      T3 = T1+T2
      print ("Joined Tuple:", T3)
      It will produce the following output
      Joined Tuple: (10, 20, 30, 40, 'one', 'two', 'three', 'four')

      Example 2

      You can also use the augmented concatenation operator with the "+=" symbol to append T2 to T1
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      T1+=T2
      print ("Joined Tuple:", T1)

      Example 3

      The same result can be obtained by using the extend() method. Here, we need cast the two tuple objects to lists, extend so as to add elements from one list to another, and convert the joined list back to a tuple.
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      L1 = list(T1)
      L2 = list(T2)
      L1.extend(L2)
      T1 = tuple(L1)
      print ("Joined Tuple:", T1)

      Example 4

      Python's built-in sum() function also helps in concatenating tuples. We use an expression
      sum((t1, t2), ())
      The elements of the first tuple are appended to an empty tuple first, and then elements from second tuple are appended and returns a new tuple that is concatenation of the two.
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      T3 = sum((T1, T2), ())
      print ("Joined Tuple:", T3)

      Example 5

      A slightly complex approach for merging two tuples is using list comprehension, as following code shows
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      L1, L2 = list(T1), list(T2)
      L3 = [y for x in [L1, L2] for y in x]
      T3 = tuple(L3)
      print ("Joined Tuple:", T3)

      Example 6

      You can run a for loop on the items in second loop, convert each item in a single item tuple and concatenate it to first tuple with the "+=" operator
      T1 = (10,20,30,40)
      T2 = ('one', 'two', 'three', 'four')
      for t in T2:
      T1+=(t,)
      print (T1)

      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.