Given a dictionary, perform append of keys followed by values in list.
Input : test_dict = {“Gfg” : 1, “is” : 2, “Best” : 3}
Output : [‘Gfg’, ‘is’, ‘Best’, 1, 2, 3]
Explanation : All the keys before all the values in list.Input : test_dict = {“Gfg” : 1, “Best” : 3}
Output : [‘Gfg’, ‘Best’, 1, 3]
Explanation : All the keys before all the values in list.
Method #1 : Using list() + keys() + values()
This is one of the ways in which this task can be performed. In this, we extract keys and values using keys() and values(), convert then to list using list() and perform append in order.
Python3
# Python3 code to demonstrate working of # Append Dictionary Keys and Values ( In order ) in dictionary # Using values() + keys() + list() # initializing dictionary test_dict = {"Gfg" : 1, "is" : 3, "Best" : 2} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # + operator is used to perform adding keys and values res = list(test_dict.keys()) + list(test_dict.values()) # printing result print("The ordered keys and values : " + str(res)) |
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2]
Method #2 : Using chain() + keys() + values()
This is one of the ways in which this task can be performed. In this, we bind keys with values together in order using chain().
Python3
# Python3 code to demonstrate working of # Append Dictionary Keys and Values ( In order ) in dictionary # Using chain() + keys() + values() from itertools import chain # initializing dictionary test_dict = {"Gfg" : 1, "is" : 3, "Best" : 2} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # chain() is used for concatenation res = list(chain(test_dict.keys(), test_dict.values())) # printing result print("The ordered keys and values : " + str(res)) |
The original dictionary is : {'Gfg': 1, 'is': 3, 'Best': 2}
The ordered keys and values : ['Gfg', 'is', 'Best', 1, 3, 2]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Extract selective keys' values Including Nested Keys
- Different ways of sorting Dictionary by Keys and Reverse sorting by keys
- Python - Append Multitype Values in Dictionary
- Python | Split dictionary keys and values into separate lists
- Python program to Swap Keys and Values in Dictionary
- Python | Test if dictionary contains unique keys and values
- Python - Sort Dictionary by Values and Keys
- Python - Remove keys with Values Greater than K ( Including mixed values )
- Python | Find keys with duplicate values in dictionary
- Python | Combine two dictionary adding values for common keys
- Python - Assign values to initialized dictionary keys
- Python - Unique value keys in a dictionary with lists as values
- Python - Keys associated with Values in Dictionary
- Python - Dictionary Keys whose Values summation equals K
- Python program to group keys with similar values in a dictionary
- Python - Test if Values Sum is Greater than Keys Sum in dictionary
- Python | Reversed Order keys in dictionary
- Python - Reverse Dictionary Keys Order
- Python - Append Similar Values as Key
- Python - Extract Unique values dictionary values
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

