In this article we will discuss how to create a list of all the values in dictionary.
Suppose we have a dictionary of string and integers i.e.
# Dictionary of string and int wordFreqDic = { "Hello": 56, "at" : 23 , "test" : 43, "this" : 2, "here" : 23, "city" : 2, }
Now we want all the Values from above dictionary in a list i.e.
[2, 43, 2, 23, 56, 23]
Let’s see how to do that,
Create a list of all values in dict using dict.values()
In python, dictionary class provides a member function i.e.
dict.values()
It returns a view object / iterator to the list of all values in dictionary. We can use this object for iteration or creating new list.
Let’s use that to get the list of all values in the above dictionary.
# Create a new list from the view object returned by values() dictValues = list (wordFreqDic.values())
dictValues content will be,
[2, 43, 2, 23, 56, 23]
Creating a list of duplicate values in dictionary
Suppose instead of all values we want a list of all duplicate values from the dictionary i.e.
[23 , 2]
Let’s do that using for loop i.e.
''' Creating a list of dulicate values in dictionary ''' uniqueValues = list() duplicateValues = list() # Creating a list of all duplicate values in dictionary for x in wordFreqDic.values() : if x not in uniqueValues : uniqueValues.append(x) else: duplicateValues.append(x) print("List of Duplicate values in Dictionary" , duplicateValues)
dictkeys content will be,
[2, 23]
Python Dictionary Tutorial - Series:
- What is a Dictionary in Python & why do we need it?
- Creating Dictionaries in Python
- Iterating over dictionaries
- Check if a key exists in dictionary
- Check if a value exists in dictionary
- Get all the keys in Dictionary
- Get all the Values in a Dictionary
- Remove a key from Dictionary
- Add key/value pairs in Dictionary
- Find keys by value in Dictionary
- Filter a dictionary by conditions
- Print dictionary line by line
- Convert a list to dictionary
- Sort a Dictionary by key
- Sort a dictionary by value in descending or ascending order
- Dictionary: Shallow vs Deep Copy
- Remove keys while Iterating
- Get all keys with maximum value
- Merge two or more dictionaries in python
Subscribe with us to join a list of 2000+ programmers and get latest tips & tutorials at your inbox through our weekly newsletter.
Complete example is as follows,
def main(): # Dictionary of string and int wordFreqDic = { "Hello": 56, "at" : 23 , "test" : 43, "this" : 2, "here" : 23, "city" : 2, } print(wordFreqDic) ''' Creating a list of all values in dictionary ''' # Create a new list from the view object returned by values() dictValues = list (wordFreqDic.values()) print("List of values in Dictionary : ", dictValues) ''' Creating a list of dulicate values in dictionary ''' uniqueValues = list() duplicateValues = list() # Creating a list of all duplicate values in dictionary for x in wordFreqDic.values() : if x not in uniqueValues : uniqueValues.append(x) else: duplicateValues.append(x) print("List of Duplicate values in Dictionary" , duplicateValues) if __name__ == '__main__': main()
Output:
{'city': 2, 'test': 43, 'this': 2, 'at': 23, 'Hello': 56, 'here': 23} List of values in Dictionary : [2, 43, 2, 23, 56, 23] List of Duplicate values in Dictionary [2, 23]
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.