Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python slice dictionary

# Import the itertools libarary 
# This library ships with Python 3 so you don't have to download anything
# It provides a bunch of tools for dealing with sequences and iteration
import itertools

# Create a new dictionary variable
myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

# Create a sliced dictionary
# dict() is used to convert the iterable result of itertools.islice() to a new dictionary
slicedDict = dict(itertools.islice(myDictionary.items(), 1 ,3))

print(slicedDict)

# Prints out 
# {   
#     'animal': 'cat', 
#     'colour': 'red'
# }
Comment

python slice a dict

#python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]

#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = {  v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = {  v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}

#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = {  v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}

#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}

#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#refer to #python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]

#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = {  v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = {  v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}

#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = {  v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}

#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}

#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#refer to https://ideone.com/uufYuP
#dn't forget to upvote
Comment

PREVIOUS NEXT
Code Example
Python :: python list prime numbers 
Python :: check integer number python 
Python :: django setup in windows 
Python :: how to import sin and cos in python 
Python :: play sound on python 
Python :: list of seaborn color palette 
Python :: mongo db python 
Python :: convert list to nd array 
Python :: python how to get user input 
Python :: search dictionary for value 
Python :: python line_profiler 
Python :: How do you print a integer in python 
Python :: discord.py read embed on message 
Python :: sort a list of array python 
Python :: dataframe to text file 
Python :: python import file from parent directory 
Python :: python print odd numberrs 
Python :: how to remove tkinter icon 
Python :: comment in python 
Python :: django filter by date range 
Python :: df dropna 
Python :: debug mode: on flask pythin window 
Python :: string to bits python 
Python :: odd or even in python 
Python :: python find smallest value in 2d list 
Python :: python grid 
Python :: networkx draw graph with weight 
Python :: tensor vs numpy array 
Python :: Write a Python program to count the number of lines in a text file. 
Python :: how to check for missing values in pandas 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =