Programmer's Python Data - Sequences
Written by Mike James   
Monday, 11 December 2023
Article Index
Programmer's Python Data - Sequences
Slices
Modifying Sequences

Modifying Sequences

No, it’s not a paradox or a contradiction. The tuple is immutable and you can’t change it after it is defined, but the need to modify a tuple, and other immutable sequences, is a common one. This sometimes worries beginners, but changing immutable data is very easy – you simply create a new data object with the desired elements. For example, if you want to delete the ith element of a tuple you would use

u = t[:i]+t[i+1:]

You can see that u is a new tuple built by concatenating two slices of t. Clearly t is still immutable, but if you write:

t = t[:i]+t[i+1:]

it does look as if t has been changed. What has happened, of course, is that t is set to reference the new tuple and the original tuple that t references will be garbage collected as long as there is no other variable referencing it.

Similarly if you want to add an element to a tuple you can use:

t = t[:i]+(42,)+t[i+1:]

where in this case you need to use a trailing comma to create a tuple with one element.

In general, to manipulate a tuple, or any immutable data structure, you simply take the data apart and then put it back together to create a new data structure, see String Manipulation in the next chapter for more examples.

In chapter but not in this extract

  • Tuples
  • Modifying Immutable Sequences
  • Destructuring
  • Sorting & Order
  • Ranges

 

Summary

  • A sequence isn’t like a data type encountered in other languages. It is more a mixin that can be added to or found in a range of different data types.

  • The archetypal sequence is the list, closely followed by the tuple.

  • All sequences can be indexed and sliced.

  • Indexing selects a particular element of the sequence and if it doesn’t exist throws an exception.

  • Slicing extracts or modifies a subsequence and never throws an exception.

  • Sequences also support a range of useful methods including in which can test to see if an object is part of a sequence.

  • A tuple is an immutable list.

  • Even though a tuple is immutable it can appear to be modified by creating new tuples derived from existing tuples.

  • A sequence is always an iterable

  • Destructuring can be used to access the elements in a list, tuple or, in general, any iterable.

  • Being able to sort its element is specific to lists and tuples.

  • The range sequence is a functional implementation of a sequence and is useful whenever you need to create an integer sequence.

Programmer's Python
Everything is Data

Is now available as a print book: Amazon

pythondata360Contents

  1. Python – A Lightning Tour
  2. The Basic Data Type – Numbers
       Extract: Bignum
  3. Truthy & Falsey
  4. Dates & Times
       Extract Naive Dates
  5. Sequences, Lists & Tuples
       Extract Sequences 
  6. Strings
       Extract Unicode Strings
  7. Regular Expressions
       Extract Simple Regular Expressions 
  8. The Dictionary
       Extract The Dictionary 
  9. Iterables, Sets & Generators
       Extract  Iterables 
  10. Comprehensions
       Extract  Comprehensions 
  11. Data Structures & Collections
       Extract Stacks, Queues and Deques
      
    Extract Named Tuples and Counters
  12. Bits & Bit Manipulation
       Extract Bits and BigNum 
       Extract Bit Masks
  13. Bytes
       Extract Bytes And Strings
       Extract Byte Manipulation 
  14. Binary Files
       Extract Files and Paths 
  15. Text Files
       Extract Text Files & CSV 
       Extract JSON ***NEW!!!
  16. Creating Custom Data Classes
        Extract A Custom Data Class 
  17. Python and Native Code
        Extract   Native Code
    Appendix I Python in Visual Studio Code
    Appendix II C Programming Using Visual Studio Code

<ASIN:1871962765>

<ASIN:1871962749>

<ASIN:1871962595>

<ASIN:B0CK71TQ17>

<ASIN:187196265X>

Related Articles

Creating The Python UI With Tkinter

Creating The Python UI With Tkinter - The Canvas Widget

The Python Dictionary

Arrays in Python

Advanced Python Arrays - Introducing NumPy

pico book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner



Last Updated ( Monday, 11 December 2023 )