- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Ways to create a dictionary of Lists
A dictionary is a collection which is unordered, changeable and indexed. In Python dictionaries are written with curly brackets, and they have keys and values. You can access the items of a dictionary by referring to its key name, inside square brackets.
Example
# Creating an empty dictionary myDict = {} # Adding list as value myDict["key1"] = [1, 2] myDict["key2"] = ["Vishesh", "For", "Python"] print(myDict) # Creating an empty dictionary myDict = {} # Adding list as value myDict["key1"] = [1, 2] # creating a list lst = ['vishesh', 'For', 'python'] # Adding this list as sublist in myDict myDict["key1"].append(lst) print(myDict) # Creating an empty dict myDict = dict() # Creating a list valList = ['1', '2', '3'] # Iterating the elements in list for val in valList: for ele in range(int(val), int(val) + 2): myDict.setdefault(ele, []).append(val) print(myDict) # Creating a dictionary of lists using list comprehension d = dict((val, range(int(val), int(val) + 2)) for val in ['1', '2', '3']) print(d)
Output
{'key2': ['Vishesh', 'For', 'Python'], 'key1': [1, 2]} {'key1': [1, 2, ['vishesh', 'For', 'python']]} {1: ['1'], 2: ['1', '2'], 3: ['2', '3'], 4: ['3']} {'1': [1, 2], '3': [3, 4], '2': [2, 3]}
- Related Articles
- In Python how to create dictionary from two lists?
- Python - Ways to Copy Dictionary
- Python - Ways to invert mapping of dictionary
- Python - Ways to iterate tuple list of lists
- Python - Ways to remove a key from dictionary
- Convert two lists into a dictionary in Python
- How to map two lists into a dictionary in Python?
- Python – Cross mapping of Two dictionary value lists
- How to create a dictionary in Python?
- How to create Python dictionary from the value of another dictionary?
- How to create nested Python dictionary?
- Python program to create a dictionary from a string
- Program to find number of ways to form a target string given a dictionary in Python
- How to create a Python dictionary from text file?
- Python program to create a sorted merged list of two unsorted lists

Advertisements