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
      Change List Items

      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.

      Change List Items

      List is a mutable data type in Python. It means, the contents of list can be modified in place, after the object is stored in the memory. You can assign a new value at a given index position in the list

      Syntax

      list1[i] = newvalue

      Example 1

      In the following code, we change the value at index 2 of the given list.
      list3 = [1, 2, 3, 4, 5]
      print ("Original list ", list3)
      list3[2] = 10
      print ("List after changing value at index 2: ", list3)
      It will produce the following output
      Original list [1, 2, 3, 4, 5]
      List after changing value at index 2: [1, 2, 10, 4, 5]
      You can replace more consecutive items in a list with another sublist.

      Example 2

      In the following code, items at index 1 and 2 are replaced by items in another sublist.
      list1 = ["a", "b", "c", "d"]
      
      print ("Original list: ", list1)
      
      list2 = ['Y', 'Z']
      list1[1:3] = list2
      
      print ("List after changing with sublist: ", list1)
      It will produce the following output
      Original list: ['a', 'b', 'c', 'd']
      List after changing with sublist: ['a', 'Y', 'Z', 'd']

      Example 3

      If the source sublist has more items than the slice to be replaced, the extra items in the source will be inserted. Take a look at the following code −
      list1 = ["a", "b", "c", "d"]
      print ("Original list: ", list1)
      list2 = ['X','Y', 'Z']
      list1[1:3] = list2
      print ("List after changing with sublist: ", list1)
      It will produce the following output −
      Original list: ['a', 'b', 'c', 'd']
      List after changing with sublist: ['a', 'X', 'Y', 'Z', 'd']

      Example 4

      If the sublist with which a slice of original list is to be replaced, has lesser items, the items with match will be replaced and rest of the items in original list will be removed.
      In the following code, we try to replace "b" and "c" with "Z" (one less item than items to be replaced). It results in Z replacing b and c removed.
      list1 = ["a", "b", "c", "d"]
      print ("Original list: ", list1)
      list2 = ['Z']
      list1[1:3] = list2
      print ("List after changing with sublist: ", list1)
      It will produce the following output
      Original list: ['a', 'b', 'c', 'd']
      List after changing with sublist: ['a', 'Z', 'd']

      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.