How to make a dictionary from two lists in python

Home /

Table of Contents

Brief about python dictionary

In Python, a dictionary is a collection of key-value pairs, where each key is unique. Dictionaries are also known as associative arrays, maps, or hash maps. They are similar to lists or arrays in other programming languages, but instead of using integers as indices to access elements, they use keys.

Dictionaries are implemented as hash tables, which allows for efficient lookups and insertion of items. They are also known as associative arrays or hash maps in other programming languages.

You can create a dictionary in Python by enclosing a comma-separated list of key-value pairs in curly braces {} or by using the dict() constructor.

To create a dictionary using different lists of keys and values

You can use the ‘zip’ function to combine the two lists into a list of tuples, and then pass that list to the ‘dict’ constructor to create a dictionary.Each tuple contains a key-value pair. This list of tuples is then passed to the dict constructor, which creates a new dictionary with the keys and values from the tuples. Here’s an example:

zip function:

zip(*iterables, strict=False)

Iterate over several iterables in parallel, producing tuples with an item from each one.

Code:

Python
keys = ['a', 'b', 'c']
values = [1, 2, 3]
d = dict(zip(keys, values))
print(d)

Output:

{'a': 1, 'b': 2, 'c': 3}

Alternatively, you can use a dictionary comprehension, like this:

Code:

Python
keys = ['a', 'b', 'c']
values = [1, 2, 3]
d = {keys[i]: values[i] for i in range(len(keys))}
print(d)

Output:

{'a': 1, 'b': 2, 'c': 3}

The above code creates a new dictionary using dictionary comprehension, where it iterates over the range of length of keys and assigns the values of keys[i] as key and values[i] as value to the dictionary.

You can access the values in the dictionary using the keys:

Python
print(d['a'])

Output:

1

Make two List from Dictionary Keys

To make two lists from the keys of a dictionary in Python, you can use the ‘keys()‘ method of the dictionary object and then convert the resulting view object to a list. You can then use slicing or other methods to split the list into two separate lists. Here is an example:

Python
# Sample dictionary
my_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# Get the keys as a list
keys_list = list(my_dict.keys())

# Split the list into two separate lists
list1 = keys_list[:len(keys_list)//2]
list2 = keys_list[len(keys_list)//2:]

# Print the lists
print(list1)
print(list2)

In the above example, the ‘keys()‘ method returns a view object which is then converted to a list using the ‘list()‘ function. The resulting ‘keys_list‘ contains all the keys from the dictionary. The ‘len(keys_list)//2‘ expression is used to calculate the mid-point of the list, which is then used to split the list into two halves. Finally, the two lists are printed using the ‘print()‘ function.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article