Learn basics of python collection data type (list, tuple, dictionary and set) instantly

 

 

List

Tuple

Dictionary

Set

Definition

Its a mutable/changeable object and its collection is ordered.

 

Allow duplicate members

 

Note:

Its changeable/ immutable object.(i.e, able to update,add or remove the list  item)

Its a immutable/un changeable  object and its collection is ordered.

 

Allow duplicate members

 

Note:

Its unchangeable/ immutable object.(i.e, unable to update, add, remove the tuple item)

 

However, we can  delete items from it.

Its a mutable/ changeable  object and its collection is unordered.

 

No duplicate members

 

Note:

Its changeable/ immutable object.(i.e, able to update,add or remove the dictionary item)

 

 

Its  a collection of unordered item.

 

No duplicate members

 

Its used to perform mathematical set operations like union, intersection, symmetric difference, etc.

 

Note:

A set itself may be modified, but the items contained in the set must be of an immutable/non changeable.

Denoted by

Its denoted by []

 

For example:

Fruits =[‘apple’, ‘banana’]

Its denoted by ()

 

For example:

Fruits =(‘apple’, ‘banana’)

Its denoted by  {key:  value}

 

For example:

Fruits={‘apple’:’red’,

’banana’:’yellow’}

Its denoted by  {}

 

For example:

Fruits={‘apple’,

’banana’}

Accessed by

Its  accessed by index

 

For example:

 

Fruits =[‘apple’, ‘banana’]

print(Fruit[0])


Output: apple

 

Note: List index starts with 0

Its  accessed by index

 

For example:

 

Fruits =(‘apple’, ‘banana’)

print(Fruit[1])

 

Output: banana

 

Note: Tuple index starts with 0

Its  accessed by using key in index

 

For example:

 

Fruits={‘apple’:’red’,

’banana’:’yellow’}

print(Fruit[‘apple’])

 

Output: yellow

 

Note: It’s a key and value pair.

 

It return values of when its key indexed

Its cannot be accessed by index or value.

 

But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.

 

For example:

 

Fruit= {"apple", "banana"}

for x in Fruit:

  print(x)


output:

apple

banana


 (or)


 Fruit= {"apple", "banana", "cherry"}

print("banana" in Fruit)

 output:

True

 

Comments

Popular posts from this blog

Switch frame in selenium python

Python property method