Showing posts with label PP UNIT III. Show all posts
Showing posts with label PP UNIT III. Show all posts

Sunday, August 24, 2025

List, Tuple, Set, and Dictionary in Python

 

List, Tuple, Set, and Dictionary in Python

In Python, listtupleset, and dictionary are fundamental built-in data structures used to store collections of data. They differ in characteristics such as order, mutability, and whether they allow duplicate elements or store key-value pairs.

Key Difference between List, Tuple, Set, and Dictionary in Python

Mutability:

  • List: Mutable (modifiable).
  • Tuple: Immutable (non-modifiable).
  • Set: Mutable, but elements inside must be immutable.
  • Dictionary: Mutable; keys are immutable, but values can change.

 

Order:

  • List: Maintains order of elements.
  • Tuple: Maintains order of elements.
  • Set: No guaranteed order.
  • Dictionary: As of Python 3.7+, insertion order is preserved.

 

Uniqueness:

  • List: Allows duplicates.
  • Tuple: Allows duplicates.
  • Set: Only unique elements.
  • Dictionary: Unique keys, values can be duplicated.

 

Data Structure:

  • List: Ordered collection.
  • Tuple: Ordered collection.
  • Set: Unordered collection.
  • Dictionary: Collection of key-value pairs.

 

 

 

Difference between List VS Tuple VS Set VS Dictionary in Python

Parameters

List

Tuple

Set

Dictionary

Definition

A list is an ordered, mutable collection of elements.

A tuple is an ordered, immutable collection of elements.

A set is an unordered collection of unique elements.

A dictionary is an unordered collection of key-value pairs.

Syntax

Syntax includes square brackets [ , ] with ‘,’ separated data.

Syntax includes curved brackets ( , ) with ‘,’ separated data.

Syntax includes curly brackets { , } with ‘,’ separated data.

Syntax includes curly brackets { , } with ‘,’ separated key-value data.

Creation

A list can be created using the list() function or simple assignment to [].

Tuple can be created using the tuple() function.

A set dictionary can be created using the set() function.

A dictionary can be created using the dict() function.

Empty Data Structure

An empty list can be created by l = [].

An empty tuple can be created by t = ().

An empty set can be created by s = set().

An empty dictionary can be created by {}.

Order

It is an ordered collection of data.

It is also an ordered collection of data.

It is an unordered collection of data.

Ordered collection in Python version 3.7, unordered in Python Version=3.6.

Duplicate Data

Duplicate data entry is allowed in a List.

Duplicate data entry is allowed in a Tuple.

All elements are unique in a Set.

Keys are unique, but two different keys CAN have the same value.

Indexing

Has integer based indexing that starts from ‘0’.

Also has integer based indexing that starts from ‘0’.

Does NOT have an index based mechanism.

Has a Key based indexing i.e. keys identify the value.

Addition

New items can be added using the append() method.

Being immutable, new data cannot be added to it.

The add() method adds an element to a set.

update() method updates specific key-value pair.

Deletion

Pop() method allows deleting an element.

Being immutable, no data can be popped/deleted.

Elements can be randomly deleted using pop().

pop(key) removes specified key along with its value.

Sorting

sort() method sorts the elements.

Immutable, so sorting method is not applicable.

Unordered, so sorting is not advised.

Keys are sorted by using the sorted() method.

Search

index() returns index of first occurrence.

index() returns index of first occurrence.

Unordered, so searching is not applicable.

get(key) returns value against specified key.

Reversing

reverse() method reverses the list.

Immutable, so reverse method is not applicable.

Unordered, so reverse is not advised.

No integer-based indexing, so no reversal.

Count

count() method returns occurrence count.

count() method returns occurrence count.

count() not defined for sets.

count() not defined for dictionaries.

 

List

Lists are one of the most commonly used data structures provided by python; they are a collection of iterable, mutable and ordered data. They can contain duplicate data.

Certainly! Here are the code snippets extracted and organized with suitable subheadings for each data structure:

Syntax

Code:

list1 = [1 , 2, 'abc', 3, 'def']

list2 = []

list3 = list((1, 2, 3))

print(list1)

print(list2)

print(list3)

 Output:

[1, 2, 'abc', 3, 'def']

[]

[1, 2, 3]


Indexing

Code:

list1 = [1, 2, "hello", 3]

print(list1[2])

 Output:

'hello'


Adding New Element

Code:

list1 = ["one", "two", "three"]

list1.append("four")

print(list1)

 Output:

['one', 'two', 'three', 'four']


Deleting Element

Code:

list1 = ["one", "two", "three", "four"]

list1.pop()

print(list1)

Output:

['one', 'two', 'three']


Sorting Elements

Code:

list1 = [100, 3, 4, 12, 1]

list1.sort()

print(list1)

 

Output:

[1, 3, 4, 12, 100]


Searching Elements

Code:

list1 = [100, 3, 4, 12, 1]

# index() returns index for 4 in list1

print(list1.index(4))

 

Output:

2

Reversing Elements

Code:

list1 = [100, 3, 4, 12, 1]

list1.reverse()

print(list1)

 

Output:

[1, 12, 4, 3, 100]


Counting Elements

Code:

list1 = [1, 2, 3, 6, 2, 2, 2]

print(list1.count(2))

 

Output:

4

 

Tuple

Tuples are similar to lists. This collection also has iterable, ordered, and (can contain) repetitive data, just like lists. But unlike lists, tuples are immutable.

Syntax

Code:

tuple1 = (1, 2, 'abc', 3, 4)

tuple2 = ()

tuple3 = tuple((1, 3, 5, "hello"))

print(tuple1)

print(tuple2)

print(tuple3)

 Output:

(1, 2, 'abc', 3, 4)

()

(1, 2, 3, 'hello')

 

Indexing

 

Code:

tuple1 = (1, 2, "hello", 3)

print(tuple1[2])

 Output:

'hello'

Set

Set is another data structure that holds a collection of unordered, iterable and mutable data. But it only contains unique elements.

Certainly! Here are the code snippets for the "Set" section of the table, including the Syntax and Searching Elements sections:

 

Syntax

Code:

set1 = {1, 2, 3, 'abc', 6}

print(set1)

 Output:

{'abc', 1, 2, 3, 6}

 

Dictionary

Unlike all other collection types, dictionaries strictly contain key-value pairs.

  • In Python versions < 3.7: is an unordered collection of data.
  • In Python v3.1: a new type of dictionary called ‘OrderedDict’ was introduced, which was similar to dictionary in python; the difference was that orderedDict was ordered (as the name suggests)
  • In the latest version of Python, i.e. 3.7: Finally, in python 3.7, dictionary is now an ordered collection of key-value pairs. The order is now guaranteed in the insertion order, i.e. the order in which they were inserted.

Syntax

Code:

dict1 = {"key1": "value1", "key2": "value2"}

dict2 = {}

dict3 = dict({1: "one", 2: "two", 3: "three"})

print(dict1)

print(dict2)

print(dict3)

 Output:

{'key2': 'value2', 'key1': 'value1'}

{}

{1: 'one', 2: 'two', 3: 'three'}

 

Indexing

 

Code:

dict1 = {"one": 1, "two": 2, "three": 3}

print(dict1.keys())

print(dict1.values())

print(dict1['two'])

 Output:

['three', 'two', 'one']

[3, 2, 1]

2

 

Adding New Element

 

Code:

dict1 = {"India": "IN", "Russia": "RU", "Australia": "AU"}

dict1.update({"Canada": "CA"})

print(dict1)

dict1.pop("Australia")

print(dict1)

 Output:

{'Canada': 'CA', 'Australia': 'AU', 'India': 'IN', 'Russia': 'RU'}

{'Canada': 'CA', 'India': 'IN', 'Russia': 'RU'}

 

Deleting Element

 

Code:

dict1 = {"India": "IN", "Russia": "RU", "Australia": "AU"}

dict1.pop('Russia')

print(dict1)

 Output:

{'Australia': 'AU', 'India': 'IN'}


Sorting Elements


Code:

 dict1 = {"India": "IN", "Russia": "RU", "Australia": "AU"}

print(sorted(dict1))

 Output:

['Australia', 'India', 'Russia']

 

Searching Elements

 

Code:

dict1 = {"India": "IN", "Russia": "RU", "Australia": "AU"}

print(dict1['Australia'])

 Output:

AU

 

Key Differences between Dictionary, List, Set, and Tuple

Syntax


ü Dictionary: Uses curly brackets { } with key-value pairs separated by commas.

ü List: Employs square brackets [ ] with comma-separated elements.

ü Set: Utilizes curly brackets { } with comma-separated elements.

ü Tuple: Employs parentheses ( ) with comma-separated elements.

 

Order


ü Dictionary: Maintains order in Python 3.7+ but is unordered in Python 3.6.

ü List: Maintains order.

ü Set: Unordered.

ü Tuple: Maintains order.

 

Duplicate Data

 

ü Dictionary: Keys are unique, values can be duplicated.

ü List: Allows duplicate elements.

ü Set: Does not allow duplicate elements.

ü Tuple: Allows duplicate elements.

 

Indexing

 

ü Dictionary: Key-based indexing.

ü List: Integer-based indexing starting from 0.

ü Set: No index-based mechanism.

ü Tuple: Integer-based indexing starting from 0.

 

Elements


ü Dictionary: Uses key-value pairs.

ü List: New items can be added using append() method.

ü Set: Uses add() method.

ü Tuple: Being immutable, new data cannot be added.

 

Deleting Elements

 

ü Dictionary: Uses pop(key) method to remove specified key and value.

ü List: Uses pop() method to delete an element.

ü Set: Uses pop() method to remove an element.

ü Tuple: Being immutable, no data can be popped or deleted.

 

Sorting Elements

 

ü Dictionary: Keys can be sorted using the sorted() method.

ü List: Uses sort() method to sort elements.

ü Set: Unordered, so sorting is not applicable.

ü Tuple: Being immutable, data cannot be sorted.

 

Searching Elements

 

ü Dictionary: Uses the get(key) method to retrieve value for a specified key.

ü List: Uses index() method to search and return index of first occurrence.

ü Set: Unordered, so searching is not applicable.

ü Tuple: Uses index() method to search and return index of first occurrence.

 

 Reversing Elements

 

ü Dictionary: No integer-based indexing, so no reversal.

ü List: Uses reverse() method to reverse elements.

ü Set: Unordered, so reversing is not advised.

ü Tuple: Being immutable, reverse method is not applicable.

 

Counting Elements

 

ü Dictionary: count() not defined for dictionaries.

ü List: Uses count() method to count occurrence of a specific element.

ü Set: count() is not defined for sets.

ü Tuple: Uses count() method to count occurrence of a specific element.

PP UNIT IV & WEEK IV PROGRAMS

  UNIT-IV Files: Types of Files, Creating and Reading Text Data, File Methods to Read and Write Data, Reading and Writing Binary Files, Pi...