Introduction

Lists and tuples are fundamental Python data structures for storing collections of data. Lists are mutable, allowing modification after creation. Tuples, however, are immutable, meaning they cannot be changed once created. This section explores how to work with these structures.

Creating and Accessing Lists

Lists are versatile and can be used to store a collection of items (strings, numbers, or other lists).

Creating Lists

You can create a list by enclosing items in square brackets [], separated by commas.

Example:

  fruits = ['apple', 'banana', 'cherry']
print(fruits)
  

Accessing List Items

List items are indexed and can be accessed by referring to the index number, starting from zero.

Example:

  first_fruit = fruits[0]  # Accessing the first item
print("The first fruit is:", first_fruit)
  

List Comprehensions

List comprehensions provide a concise way to create lists based on existing lists.

Syntax and Explanation:

  new_list = [expression for item in iterable if condition]
  

Example:

  # Create a list of squares from 1 to 10
squares = [x**2 for x in range(1, 11)]
print(squares)
  

This example creates a list of square numbers from 1 to 10. It’s a clear and efficient way to generate a list without needing multiple lines of code for a loop.

Operations on Tuples

Tuples are similar to lists but are immutable. They are created by placing comma-separated values inside parentheses ().

Creating Tuples

  my_tuple = (1, 2, 3)
print(my_tuple)
  

Accessing Tuple Items

Tuple items are accessed similarly to list items, by using index numbers.

Example:

  print("First element of the tuple:", my_tuple[0])
  

Tuple Operations

While you cannot modify tuples, you can perform operations such as concatenation and repetition.

Example:

  # Concatenating two tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
combined_tuple = tuple1 + tuple2
print(combined_tuple)

# Repeating a tuple
repeated_tuple = tuple1 * 2
print(repeated_tuple)
  

Conclusion

Lists and tuples are integral to data handling in Python. Lists offer flexibility and a wide array of methods for manipulation, making them suitable for applications where the collection of items might change. Tuples, being immutable, are perfect for fixed data sets and can serve as keys in dictionaries. This guide has explored how to create, access, and manipulate these structures with practical examples.

Learn How To Build AI Projects

Now, if you are interested in upskilling in 2024 with AI development, check out this 6 AI advanced projects with Golang where you will learn about building with AI and getting the best knowledge there is currently. Here’s the link.

Last updated 17 Aug 2024, 12:31 +0200 . history