Source Code Snippets of "Collections.py"

Mike Dinder

The entire contents of the "Collections.py" file as code examples with syntax highlighting

Auto Generated Using Generate_HTML.py script to auto format each of these pages in a standardized template.

On smaller viewports/windows, please scroll or use your touch/finger to scroll left to right to view all the code displayed.

Collections Python Home
                                        # Various Collections and/or Sets Operations
from collections import Counter

print("Comparing Lists/Sets")

numbered_list = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
comparing_list = [3, 9, 12, 15, 20, 25, 33, 56, 99, 100]

print("Your Standard List Is:", numbered_list)
print("Your Comparable List Is:", comparing_list)

set_list_1 = set(numbered_list)
set_list_2 = set(comparing_list)
print("A) The Difference in the Two Lists is:", set_list_1.difference(set_list_2))
print("B) The Difference in the Two Lists is:", set_list_2.difference(set_list_1))

print('--------------------------------------------------')
# Counter function of collections library
print("Using collections.Counter() function.")

fruit = Counter(['Apple', 'Pineapple', 'Peach', 'Apple', 'Peach', 'Blueberry', 'Grape', 'Orange', 'Apple', 'Banana'])
animals = Counter({'Dogs': 12, 'Cats': 3, 'Cows': 44, 'Horses': 25, 'Pigs': 8, 'Chickens': 87, 'Ducks': 1})

print('Fruit Counter:', fruit)
print('Most Common Fruit:', fruit.most_common())
print(f'Access Element in Fruit, Pineapple = {fruit['Peach']}')
print('Animals Counter:', animals)
print('Most Common Animals:', animals.most_common())
print(f'Access Element in Animals, Dogs = {animals['Dogs']}')


                                        

Runs the entire script above. Open your Console Window in Inspect Code to view the output of the script above.

A prompt popup will ask you for various input() commands.

Python in Your Browser

Client-side execution via Pyodide · No server · Full stdlib · numpy, pandas, matplotlib available via micropip

Mobile users will want to use this method. Copy snippets or the entire script from above. You may also copy this script exactly as it is shown and you can run it on your own machine using the Command-Line Interface (CLI).

Loading Pyodide...
>>>
Enter run Shift+Enter new line ↑↓ history