Sunday, January 28, 2024

Set vs list vs Dictionary

 DIFFERENCES BETWEEN SET ,LIST AND DICTIONARY

FeatureSetListDictionary
DefinitionUnordered collection of unique elementsOrdered collection of elementsCollection of key-value pairs
Syntax{}[]{key1: value1, key2: value2, ...}
MutableYesYesYes
OrderedNoYesNo
IndexingNoYesBy keys
ElementsUniqueDuplicates allowedKeys must be unique, values can be duplicate
AccessIterationIndexing, slicingBy key
Examplemy_set = {1, 2, 3}my_list = [1, 2, 3]my_dict = {'a': 1, 'b': 2}
Use casesRemoving duplicates, checking membershipSequences, maintaining orderAssociative arrays, fast lookup by key

This table highlights the key differences between sets, lists, and dictionaries in terms of their characteristics, syntax, mutability, ordering, indexing, access methods, and common use cases.

No comments:

Post a Comment

Input Text and Display in tkinter

  import tkinter as tk def display_text():     text = entry.get()     label.config(text="You entered: " + text) root = tk.Tk() roo...