Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python get first n elements of dict

"""
NOTE: dictionaries do NOT remember the order in which 
key : value pairs have been stored
"""
d = {"a" : 5, "b" : 1, "c" : 8, "d" : 4, "e" : 3, "f" : 10, "g": 2}
# getting the first n key : value pairs
n = 3
first_n = dict(zip(list(d.keys())[:n], list(d.values())[:n]))
first_n
>>> {'a': 5, 'b': 1, 'c': 8}
Comment

PREVIOUS NEXT
Code Example
Python :: import counter python 
Python :: convert array to dataframe python 
Python :: check pip installed packages inside virtualenv 
Python :: drop na in pandas 
Python :: vscode not recognizing python import 
Python :: join on column pandas 
Python :: how to find columns of a dataframe 
Python :: trump 
Python :: How to Copy a File in Python? 
Python :: how to split string with comma in python 
Python :: python negation of an statement 
Python :: how to read a .exe file in python 
Python :: python truncate to integer 
Python :: pycharm remove not in use imports 
Python :: Geopandas to SHP file 
Python :: plot confidence interval matplotlib 
Python :: python loop through list 
Python :: pyaudio install error ubuntu 
Python :: Concatenate strings using Pandas groupby 
Python :: print random word py 
Python :: all alphabets 
Python :: qlabel alignment center python 
Python :: get rid of n in string python 
Python :: how to load wav file python 
Python :: flask debug 
Python :: convert set to list python time complexity 
Python :: python multiply one column of array by a value 
Python :: TypeError: dict is not a sequence 
Python :: np.loadtext 
Python :: how to make an object set once python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =