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

access first element of dictionary python

res = next(iter(test_dict))
Comment

access first element of dictionary python


my_dict = {'foo': 'bar', 'spam': 'eggs'}
next(iter(my_dict)) # outputs 'foo'

Comment

PREVIOUS NEXT
Code Example
Python :: selection sort python 
Python :: python socket check if still connected 
Python :: how to use a string variable as a variable name in python 
Python :: python convert string datetime into datetime 
Python :: get sum from x to y in python 
Python :: python numphy how to use fractions 
Python :: python resize image in tkinter 
Python :: how to create window in tkinter 
Python :: install python 3.6 on centos 
Python :: add time to a datetime object 
Python :: create a new dataframe from existing dataframe pandas 
Python :: How to do an infinte while in python 
Python :: how to fetch all chars of a string before a space in python 
Python :: build dataframe from dictionary 
Python :: python ternary 
Python :: multinomial regression scikit learn 
Python :: python3 strip punctuation from string 
Python :: python get last element of iterator 
Python :: numpy inverse square root 
Python :: run multiple function with multiprocessing python 
Python :: Get Current Date using today method 
Python :: python password hashing 
Python :: # time delay in python script 
Python :: python convert string to lowercase 
Python :: heroku python version 
Python :: delete nans in df python 
Python :: converting decimal to hex in python 
Python :: reportlab page size a4 
Python :: union dataframe pyspark 
Python :: how to urllib3 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =