Working with Lists and Dictionaries in Python

December 8, 2023

Lists and dictionaries are two of the most commonly used data structures in Python. These data structures allow us to store and organize data in a way that is easily accessible and modifiable. In this post, we will cover how to create, access, and manipulate lists and dictionaries, as well as advanced techniques like list comprehension and dictionary comprehension.

Lists

A list is a collection of items that are ordered and mutable, meaning they can be modified after creation. Lists are denoted by square brackets [] and can contain any type of data, including other lists. Let’s take a look at an example of how to create a list:

fruits = ['apple', 'banana', 'orange', 'grape']

In the above example, we have created a list called fruits that contains four strings. We can access the items in the list by using their index, which starts at 0. For example, to access the first item in the list, we would use fruits[0] which would return 'apple'. We can also use negative indexing to access items from the end of the list. For example, fruits[-1] would return 'grape'.

Lists also have a variety of built-in methods that allow us to manipulate them. Some commonly used methods include append(), insert(), remove(), and sort(). Let’s take a look at an example of how to use these methods:

# add 'kiwi' to the end of the list
fruits.append('kiwi')

# insert 'pineapple' at index 2
fruits.insert(2, 'pineapple')

# remove 'orange' from the list
fruits.remove('orange')

# sort the list in alphabetical order
fruits.sort()

These are just a few of the many methods available for lists. For a full list of methods, check out the official Python documentation.

Dictionaries

A dictionary is a collection of key-value pairs that are unordered and mutable. Dictionaries are denoted by curly braces {} and each key-value pair is separated by a colon :. Let’s take a look at an example of how to create a dictionary:

person = {'name': 'John', 'age': 30, 'occupation': 'teacher'}

In the above example, we have created a dictionary called person that contains three key-value pairs. We can access the value of a specific key by using its key name, for example person['name'] would return 'John'. We can also use the get() method to access values, which allows us to specify a default value if the key does not exist. For example, person.get('city', 'unknown') would return 'unknown' since the key 'city' does not exist in the dictionary.

Similar to lists, dictionaries also have built-in methods for manipulation, such as update(), pop(), and clear(). Let’s see how these methods work:

# add 'city' key with value 'New York'
person['city'] = 'New York'

# remove 'occupation' key and return its value
occupation = person.pop('occupation')

# clear all key-value pairs from the dictionary
person.clear()

For a full list of methods and their usage, refer to the official Python documentation.

List Comprehension and Dictionary Comprehension

List comprehension and dictionary comprehension are advanced techniques that allow us to create lists and dictionaries in a concise and efficient way. Let’s take a look at an example of how to use list comprehension to create a list of even numbers from 1 to 10:

even_numbers = [x for x in range(1, 11) if x % 2 == 0]

In the above example, we are using a for loop and an if statement to filter out the even numbers and add them to the list even_numbers. Similarly, we can use dictionary comprehension to create a dictionary with the first 5 letters of the alphabet as keys and their corresponding ASCII values as values:

ascii_dict = {chr(x): x for x in range(65, 70)}

In this example, we are using the built-in function chr() to convert the ASCII values to their corresponding characters.

These are just a few examples of how list comprehension and dictionary comprehension can be used. They are powerful tools that can greatly simplify your code, so it’s worth taking the time to learn and practice them.

Conclusion

In this post, we covered the basics of working with lists and dictionaries in Python. We learned how to create, access, and manipulate these data structures, as well as some advanced techniques like list comprehension and dictionary comprehension. By mastering these concepts, you will have a solid foundation for working with data in Python.

Thank you for reading and happy coding!