Python Track · Part 4 of 9

Python Data Structures: Lists, Sets and Comprehensions

Python
Data Structures
Work with Python lists, tuples, dictionaries, sets and frozensets, write list comprehensions, and sort and search collections.
Author

Gujuri Alekha

Published

Oct 20, 2024

4.1 Working with Lists, Tuples, and Dictionaries

  • Lists: Lists are ordered collections of items in Python. They can hold elements of different data types and are mutable, meaning you can change their contents (add, remove, modify elements).
  • Tuples: Tuples are similar to lists but are immutable, which means you cannot change their contents once they are created. They are often used for representing fixed collections of items.
  • Dictionaries: Dictionaries are collections of key-value pairs. They are unordered, mutable, and allow you to store and retrieve values using keys rather than indexes. Dictionaries are versatile and commonly used for storing and retrieving data efficiently.

4.2 List Comprehensions

  • List comprehensions are a concise and elegant way to create lists in Python. They allow you to generate a new list by applying an expression to each item in an existing iterable (e.g., a list, tuple, or range).
  • List comprehensions are written inside square brackets [] and typically consist of an expression and a for loop to iterate over the elements of an iterable.
Show code
squares = [x**2 for x in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

4.3 Sets and Frozensets

  • Sets: Sets are unordered collections of unique elements. They do not allow duplicate values. You can perform set operations like union, intersection, and difference.
  • Frozensets: Frozensets are similar to sets but are immutable, meaning you cannot change their elements after creation. They are often used as keys in dictionaries or as elements of other sets.
Show code
#  creates a set with unique values
my_set = {1, 2, 3, 3, 4,3,2,1}
print(f"My set : {my_set}")

# Creating a frozenset
my_frozenset = frozenset([1, 2, 3, 3, 4,3,2,1])
print(f"my_frozenset : {my_frozenset}")
My set : {1, 2, 3, 4}
my_frozenset : frozenset({1, 2, 3, 4})

4.4 Dealing with Collections (Sorting, Searching)

  • Sorting: You can sort collections like lists using the sorted() function or by calling the sort() method for lists in-place. You can also specify custom sorting criteria using the key argument.
  • Searching: You can use various methods to search for elements in collections, such as the in keyword to check for membership, and the index() method to find the index of a specific element in a list. For dictionaries, you can use keys to access values efficiently.
Show code
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

# Sorts the list
sorted_numbers = sorted(numbers)
print({f"sorted_numbers : {sorted_numbers}"})
{'sorted_numbers : [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]'}
Show code
fruits = ["apple", "banana", "cherry", "date"]  # Create a list
print("banana" in fruits)                        # Check if "banana" is in the list
index = fruits.index("cherry")                   # Find the index of "cherry"
print("index of cheery is {index}")
True
index of cheery is {index}

Citation

BibTeX citation:
@online{alekha2024,
  author = {Alekha, Gujuri},
  title = {Python {Data} {Structures:} {Lists,} {Sets} and
    {Comprehensions}},
  date = {2024-10-20},
  url = {https://gujurialekha.github.io/blog/python/04-python-data-structures-and-collections.html},
  langid = {en}
}