Tuples, Sets and Dictionaries Reading Material

 

1. What is a Tuple?

A tuple holds an ordered collection of items. A tuple is an immutable object, i.e. we cannot change the items of the tuple.

Creating a Tuple

  • Created by enclosing elements within (round) brackets.
  • Each item is separated by a comma.

Code

a = 2
tuple_a = (5, "Six", a, 8.2)
print(type(tuple_a))
print(tuple_a)
PYTHON

Output

<class 'tuple'>
(5, 'Six', 2, 8.2)

Tuple with a Single Item

Code

a = (1,)
print(type(a))
print(a)
PYTHON

Output

<class 'tuple'>
(1,)

Accessing Tuple Elements

Accessing

tuple
elements is also similar to string and list accessing.

Code

a = 2
tuple_a = (5, "Six", a, 8.2)
print(tuple_a[1])
PYTHON

Output

Six

2. Are Tuples mutable?

No, Tuples are immutable. They cannot be modified.

Code

tuple_a = (1, 2, 3, 5)
tuple_a[3] = 4
print(tuple_a)
PYTHON

Output

TypeError: 'tuple' object does not support item assignment

3. How to Unpack a tuple?

The values of any sequence can be directly assigned to variables. The number of variables on the left should match the length of the sequence.

Code

tuple_a = ('R', 'e', 'd')
(s_1, s_2, s_3) = tuple_a
print(s_1)
print(s_2)
print(s_3)
PYTHON

Output

R
e
d

4. What is the difference between List and Tuple?

Basis for comparisonListTuple
TypeLists are mutableTuples are immutable
Time ConsumptionThe list iteration is much slower compared to the tupleThe tuple iteration is much faster compared to the list
Appropriate UsageIt is very helpful in the case of deletion and insertion operationsIt is comparatively helpful in the case of read-only operations, such as accessing elements

5. What are Sets?

Sets are the unordered collection of items.

Every set element is

  • Unique (no duplicates)
  • Must be immutable

Creating a Set

  • Created by enclosing elements within {curly} brackets.
  • Each item is separated by a comma.

Code

a = 2
set_a = {5, "Six", a, 8.2}
print(type(set_a))
print(set_a)
PYTHON

Output

<class 'set'>
{8.2, 2, 'Six', 5}

Set items need not be in the same order as defined.

No Duplicate Items

Sets contain unique elements

Code

set_a = {"a", "b", "c", "a"}
print(set_a)
PYTHON

Output

{'b', 'a', 'c'}

6. What is a Dictionary?

A Dictionary is an unordered collection of items. Every dictionary item is a Key-Value pair.

7. How to create a Dictionary?

A dictionary is created by enclosing items within {curly} brackets.

Each item in the dictionary has a Key-Value pair separated by a comma.

Code

dict_a = {
"name": "Teja",
"age": 15
}
PYTHON

In the above dictionary, the

  • keys
     are 
    name
     and 
    age
  • values
     are 
    Teja
     and 
    15

8. How to get the keys of a dictionary?

The keys() method allows us to get the keys of dictionary. It returns a view object of the type

dict_keys
that holds a list of all keys.

Code

dict_a = {
"name": "Teja",
"age": 15
}
print(dict_a.keys())
PYTHON

Output

dict_keys(['name', 'age'])

9. Why List can't be used as a key in the dictionary/ Are the dictionary keys mutable?

A Python dictionary can have only keys of immutable types. Lists cannot be used as keys in a dictionary because they are mutable.

10. How to create an empty dictionary?

Using 
dict()
 built-in function

code

dict_a = dict()
print(type(dict_a))
print(dict_a)
PYTHON

Output

<class 'dict'>
{}

Using 
{ }
 symbol

code

dict_a = {}
print(type(dict_a))
print(dict_a)
PYTHON

Output

<class 'dict'>
{}

11. How to combine two dictionaries?

We can combine two dictionaries using

update()
method:

Syntax

dictionary.update(iterable)

code

dict_1 = {'a': 1, 'b': 2}
dict_2 = {'c': 3, 'd': 4}
dict_1.update(dict_2)
print(dict_1)
PYTHON

Output

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

12. What are the differences between Lists, Tuples, Sets and Dictionaries?

ListsTuplesSetsDictionaries
A list is an ordered collection of dataA tuple is an ordered collection of dataA set is an unordered collectionA dictionary is an unordered collection of data that stores data in Key-Value pairs
Lists are mutableTuples are immutableSets are mutableDictionaries are mutable
Lists are enclosed within square braces
[]
Tuples are enclosed within parenthesis
()
Sets are enclosed within curly brackets
{}
Dictionaries are enclosed within curly brackets
{}
 in the form of Key-Value pairs
Example: [1, 2, 3, 4, 5]Example: (1, 2, 3, 4, 5)Example: {1, 2, 3, 4, 5}Example: { a:1, b:2, c:3, d:4, e:5}
List allows duplicate elementsTuple allows duplicate elementsSet will not allow duplicate elementsDictionary doesn’t allow duplicate keys
List can be created using 
list()
 function
Tuple can be created using 
tuple()
 function
Set can be created using 
set()
 function
Dictionary can be created using 
dict()
 function

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form