Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Print a specific value of dictionary

# A simple dictionary
x = {'X':"yes", 'Y':"no", 'Z':"ok"}

# To print a specific key (for example key at index 1)
print([key for key in x.keys()][1])

# To print a specific value (for example value at index 1)
print([value for value in x.values()][1])

# To print a pair of a key with its value (for example pair at index 2)
print(([key for key in x.keys()][2], [value for value in x.values()][2]))

# To print a key and a different value (for example key at index 0 and value at index 1)
print(([key for key in x.keys()][0], [value for value in x.values()][1]))

# To print all keys and values concatenated together
print(''.join(str(key) + '' + str(value) for key, value in x.items()))

# To print all keys and values separated by commas
print(', '.join(str(key) + ', ' + str(value) for key, value in x.items()))

# To print all pairs of (key, value) one at a time
for e in range(len(x)):
    print(([key for key in x.keys()][e], [value for value in x.values()][e]))

# To print all pairs (key, value) in a tuple
print(tuple(([key for key in x.keys()][i], [value for value in x.values()][i]) for i in range(len(x))))
Comment

PREVIOUS NEXT
Code Example
Python :: time.sleep() faster 
Python :: django secure variable 
Python :: calculate angle between 3 points python 
Python :: python function as parameter 
Python :: format list into string python 
Python :: python tkinter scrollbar widget 
Python :: remove extra spaces python 
Python :: fizzbuzz python solution 
Python :: opencv erosion 
Python :: python how to import library absoluth path 
Python :: smallest program to make diamond python 
Python :: pandas backfill 
Python :: df col to dict 
Python :: import gensim 
Python :: python zeros to nan 
Python :: keras example 
Python :: pygame key pressed once 
Python :: convert price to float python 
Python :: python glob 
Python :: pandas get day names 
Python :: python sort dictionary by key 
Python :: python replace character in string 
Python :: how to find the last item of a list 
Python :: json url to dataframe python 
Python :: tensorflow_version 
Python :: NumPy unique Syntax 
Python :: How to scale a pandas dataframe 
Python :: python summary() 
Python :: error handling in python using flask 
Python :: convert a pdf folder to excell pandas 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =