Browse Courses

List and Tuples

This document explains Python lists and tuples, including indexing, slicing mutability, concatenation, nesting, methods, and aliasing, with practical examples for data manipulation.

This document covers Python lists and tuples, including indexing, slicing, mutability, concatenation, nesting, methods, and aliasing, with practical examples for data manipulation and structure.


Introduction

Lists and tuples are compound data types and key data structures in Python. Both are ordered sequences, but differ in mutability and usage.


Tuples in Python

Tuples are ordered sequences, expressed as comma-separated elements within parentheses. They can contain different types (strings, integers, floats), but the variable type is always tuple.

Indexing and Slicing

  • Access elements by index: ratings[0], ratings[1], ratings[-1]
  • Negative indexing accesses elements from the end.
  • Slicing: ratings[:3] for first three elements, ratings[-2:] for last two elements.
  • Length: len(ratings)

Tuples are immutable; their values cannot be changed. Assigning a tuple to another variable creates a reference to the same object.

Immutability and Manipulation

  • Cannot change elements directly.
  • Must create a new tuple for changes.
  • Sorting a tuple: sorted(ratings) returns a new sorted list.
  • Tuples can be nested and accessed using standard indexing.

Lists in Python

Lists are ordered sequences represented with square brackets. Lists are mutable and can contain strings, floats, integers, and other lists or tuples.

Indexing and Slicing

  • Access elements by index: L[0], L[1], L[-1]
  • Negative indexing accesses elements from the end.
  • Slicing: L[-2:] for last two elements.
  • Length: len(L)

List Operations

  • Concatenate lists: L + L2
  • Extend list: L.extend([new1, new2])
  • Append element: L.append("A")
  • Change element: L[0] = "HardRock"
  • Delete element: del L[0]
  • Split string to list: "A B C".split() or "A,B,C".split(",")

Lists are mutable; changes affect all references unless cloned.

Aliasing and Cloning

  • Aliasing: b = a (both reference same list)
  • Changing a changes b.
  • Cloning: b = a[:] (creates a new copy)

Nesting and Indexing

Both lists and tuples can be nested. Standard indexing applies for accessing nested elements.


Conclusion

Python lists and tuples provide flexible ways to store and manipulate ordered data. Understanding mutability, indexing, slicing, and methods is essential for effective data management.


FAQ

Lists are mutable ordered sequences, while tuples are immutable ordered sequences. Both can contain multiple data types and support indexing and slicing.

Both variables reference the same list, so changes to one affect the other due to aliasing.

  1. Indexing
  2. Slicing
  3. Changing an element
  4. Concatenation
(3) Tuples are immutable and cannot be changed after creation.

The split method converts a string into a list, separating elements based on spaces or a specified delimiter.

ConceptDescription
A. Aliasing1. Creating a new copy of a list
B. Cloning2. Multiple names referencing the same object
C. Extend3. Adding multiple elements to a list
D. Append4. Adding a single element to a list
A-2, B-1, C-3, D-4.

Lists in Python can be changed after creation, while tuples cannot.

True. Lists are mutable, tuples are immutable.

Ensure the index exists in the list to avoid errors when using the del command.

The extend() method is used to add multiple elements to a list.

Both lists and tuples can be nested, and standard indexing applies for accessing nested elements.

Slicing returns a new sequence containing the selected elements, without modifying the original tuple or list.