## Dictionary Sort

0 likes • Nov 18, 2022
Python

## More Python Posts

```my_list = ["blue", "red", "green"]
#1- Using sort or srted directly or with specifc keysmy_list.sort() #sorts alphabetically or in an ascending order for numeric data my_list = sorted(my_list, key=len) #sorts the list based on the length of the strings from shortest to longest. # You can use reverse=True to flip the order
#2- Using locale and functools import localefrom functools import cmp_to_keymy_list = sorted(my_list, key=cmp_to_key(locale.strcoll))```
`import copybegining = [False,False,False,False,False,None,True,True,True,True,True]#False = black True = whiteits = def swap(layout, step):    layoutCopy = copy.deepcopy(layout)    layoutCopy[(step+step)], layoutCopy[step] = layoutCopy[step], layoutCopy[(step+step)]    return layoutCopydef isSolved(layout):    for i in range(len(layout)):        if(layout[i] == False):            return (i >= (len(layout)/2))def recurse(layout, its, steps = []):    if isSolved(layout):        its += 1        print(layout,list(x for x in steps))        return    step = None    for i in range(len(layout)):        if(layout[i] == None):            if(i >= 1): #If the empty space could have something to the left                if(layout[i - 1] == False):                    step = [-1,i]                    recurse(swap(layout,step), its, (steps+[step]))                if(i > 1): #If the empty space could have something 2 to the left                    if(layout[i - 2] == False):                        step = [-2,i]                        recurse(swap(layout,step), its, (steps+[step]))            if(i < (len(layout)-1)): #If the empty space could have something to the right                if(layout[i + 1] == True):                    step = [1,i]                    recurse(swap(layout,step), its, (steps+[step]))                if(i < (len(layout)-2)): #If the empty space could have something to the right                    if(layout[i + 2] == True):                        step = [2,i]                        recurse(swap(layout,step), its, (steps+[step]))    its += 1    #return Nonerecurse(begining,its,[])print(its)`
```# Python program to reverse a linked list# Time Complexity : O(n)# Space Complexity : O(n) as 'next'#variable is getting created in each loop.
# Node class

class Node:
# Constructor to initialize the node object	def __init__(self, data):		self.data = data		self.next = None

# Function to reverse the linked list	def reverse(self):		prev = None		current = self.head		while(current is not None):			next = current.next			current.next = prev			prev = current			current = next		self.head = prev
# Function to insert a new node at the beginning	def push(self, new_data):		new_node = Node(new_data)		new_node.next = self.head		self.head = new_node
# Utility function to print the linked LinkedList	def printList(self):		temp = self.head		while(temp):			print temp.data,			temp = temp.next

# Driver program to test above functionsllist = LinkedList()llist.push(20)llist.push(4)llist.push(15)llist.push(85)
`# Deleting all even numbers from a lista = [1,2,3,4,5]del a[1::2]print(a)`
```# Function to check Armstrong numberdef is_armstrong_number(number):    # Convert number to string to iterate over its digits    num_str = str(number)        # Calculate the sum of the cubes of each digit    digit_sum = sum(int(digit) ** len(num_str) for digit in num_str)        # Compare the sum with the original number    if digit_sum == number:        return True    else:        return False
```primes=[]products=[]