DEV Community

Cover image for Python Data Structures cheat sheets: when to use lists, sets, tuples and dictionaries.
Elizabeth Njoroge
Elizabeth Njoroge

Posted on

Python Data Structures cheat sheets: when to use lists, sets, tuples and dictionaries.

A data structure is a way of organising information and the choice depends on use. The best data structure to use depends on how you intend to access and manipulate the data, as each structure has advantages and disadvantages for different operations.

Essential Python Data Structures.
1.Lists.
Python's list works like a music playlist where there are different songs that are stored in a specific kind of order. These orders collection can be changed whenever you want.

  • How to create a list; creating a list puts items into square brackets[], separated by commas and the items can be a string, numbers or booleans.
  • How to access list items; this can be done by indexing where you take a single item from a list, have its index number inside the square bracket.
  • How to modify the lists; you can change an item by index where you assign a new value to its index if you want to replace it. You can also add items with append which adds the entire argument as a single item, insert when you want to merge two lists together and extend when you want to unpack the argument and add each element individually and these does different functions.
  • How to remove items from a list; use remove when you know the value, pop(index) when you know the position and its a yes, pop() when you want the last time, del list[index] when you know the position and its a no and clear when you want to empty the list.

2.Set in python.
Python set is an unordered collection of elements that are unique, so basically one that removes duplicates.

  • How to create a set in by placing items inside curly braces{} and is separate by commas.
  • Set Operations includes, Union which gives you all items from both sets without duplicates, Intersection gives you only the items that appear in both sets and Differences which gives you items that are in the first set but not in the second set.

When to use a list is when you care about order or duplicates but use a set when you care for uniqueness.

3.Tuples
With tuples, once you create it you cannot change it for example GPS coordinates.

  • How to create tuples; by putting items inside parentheses(), and is spared by a comma.
  • Packing is when you create a tuple by assigning multiple values to one variable and Unpacking is the reverse.

4.Dictionaries
Dictionaries is used to store key-value pairs.

  • How to create Dictionary; by using curly braces{} with the key-values being spared by colon instead of a comma.
  • Accessing and Modifying Dictionary Values; where to get a value from a dictionary, you put the key inside square brackets. To change a value, assign to that key. To add a new pair, you just assign to a key that doesn't exist yet.
  • Accessing and Modifying Dictionary Values; where to get a value from a dictionary, you put the key inside square brackets. To change a value, you assign to that key and to add a new pair, just assign to a key that doesn't exist yet.
  • Checking if a Key Exists; before accessing a key, you often want to check if it exists first. The in keyword checks for keys and not values.

Nested Dictionaries
A dictionary value can be another dictionary. This is called nesting and it lets you represent complex, structured data.

Conclusion
Dictionaries are one of the most important data structures in Python as they are highly optimised and fast.

Top comments (0)