DEV Community

Kajiru
Kajiru

Posted on

Getting Started with Python (Part 8-2): Working with Multiple Data – Tuples, Sets, and Dictionaries

Working with Multiple Data – Tuples, Sets, and Dictionaries

In this article, you’ll learn about tuples, sets, and dictionaries,

which are data structures similar to lists but with different characteristics.


Tuples

Tuples are very similar to lists.

They are written by placing values separated by commas between ( and ).

Unlike lists, tuples are immutable, meaning their values cannot be changed.

Because of this, the following example will raise errors.

member = ("Maruko", "Tama", "Maruo")
member[0] = "Batsuko"  # Error
member[1] = "Pochi"    # Error
member[2] = "Batsuo"   # Error
Enter fullscreen mode Exit fullscreen mode

Tuples are a good choice when you want to handle

data that must never be changed.


Sets

Sets are also similar to lists.

They are written by placing values separated by commas between { and }.

A key feature of sets is that they automatically remove duplicate values.

Sets do not have order, so you cannot access elements using indexes like member[0].

member = {"Maruko", "Noguchi", "Tama", "Noguchi", "Maruo", "Noguchi"}
print(member)
# {'Noguchi', 'Maruko', 'Maruo', 'Tama'}
Enter fullscreen mode Exit fullscreen mode

As you can see, the duplicated "Noguchi" values have been merged into one.


Dictionaries

As the name suggests, a dictionary works like a real dictionary.

It manages data as key–value pairs.

To access a value, you specify its corresponding key.

{ key: value }
Enter fullscreen mode Exit fullscreen mode

Here is an example of using a dictionary:

member = {"maruchan": "Maruko", "tama": "Tama", "maruo": "Maruo"}
print(member)
# {'maruchan': 'Maruko', 'tama': 'Tama', 'maruo': 'Maruo'}

# Access values by key
print(member["maruchan"])  # Maruko
print(member["tama"])      # Tama
print(member["maruo"])     # Maruo

# Update values by key
member["tama"] = "Pochi"
member["maruo"] = "Batsuo"
print(member)
# {'maruchan': 'Maruko', 'tama': 'Pochi', 'maruo': 'Batsuo'}
Enter fullscreen mode Exit fullscreen mode

Common Dictionary Methods

Dictionaries also provide several important methods.

If you remember these three, you’ll be in good shape.


keys (Get all keys)

Use keys() to get a list of all keys in a dictionary.

member = {"maruchan": "Maruko", "tama": "Tama", "maruo": "Maruo"}
print(list(member.keys()))
# ['maruchan', 'tama', 'maruo']
Enter fullscreen mode Exit fullscreen mode

values (Get all values)

Use values() to get a list of all values in a dictionary.

member = {"maruchan": "Maruko", "tama": "Tama", "maruo": "Maruo"}
print(list(member.values()))
# ['Maruko', 'Tama', 'Maruo']
Enter fullscreen mode Exit fullscreen mode

items (Get key–value pairs)

Use items() to get a list of (key, value) tuples.

member = {"maruchan": "Maruko", "tama": "Tama", "maruo": "Maruo"}
print(list(member.items()))
# [('maruchan', 'Maruko'), ('tama', 'Tama'), ('maruo', 'Maruo')]
Enter fullscreen mode Exit fullscreen mode

What’s Next?

Thank you for reading!

In the next article, we’ll learn about loops and iteration.

The next title will be:

“Getting Started with Python: Using Loops”

Stay tuned! 🚀

Top comments (0)