0% found this document useful (0 votes)
33 views

Computer Science Project: Done by Veera Bhavani XIA

This document discusses the differences between lists, tuples, and dictionaries in Python. It provides details on how to create each type of data structure and convert between them. Lists are mutable while tuples are immutable. Dictionaries store elements as key-value pairs. The document shows various ways to create, access, and convert between lists, tuples, and dictionaries using built-in functions like list(), tuple(), dict(), items(), and keys().

Uploaded by

satya sree
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Computer Science Project: Done by Veera Bhavani XIA

This document discusses the differences between lists, tuples, and dictionaries in Python. It provides details on how to create each type of data structure and convert between them. Lists are mutable while tuples are immutable. Dictionaries store elements as key-value pairs. The document shows various ways to create, access, and convert between lists, tuples, and dictionaries using built-in functions like list(), tuple(), dict(), items(), and keys().

Uploaded by

satya sree
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

COMPUTER SCIENCE

PROJECT
DONE BY
VEERA BHAVANI
XI A
Difference of Lists, Tuples and Dictionary
S.NO LISTS TUPLES DICTIONARY

1. Lists are mutable Tuples are immutable, Dictionary are


,i.e., which we can i.e., which we cannot mutable .
make changes in the make changes.
elements in the list.

2. To create a list, put a To create a tuple ,put a Dictionary can be


number of comma- number of comma- created by putting a n
separated expression separated expressions umber of curly{} and
in square[] brackets. in round() brackets. round() brackets.
Common functions used Common functions used tuple Common functions used in di:
3. in lists are: are: ctionary are:
Len(),max(),min() and tuple() Len(),get(),items(),keys(),value
Ways to create LISTS:
■ To create a list ,put a number of expressions in square brackets .
■ L=[]
■ The built-in list type object to create lists from sequence as per the syntax
as given:
■ L=list(<sequence>)
■ E.g.,l1=list(‘hello’)
■ >>> l1
■ [‘h’,’e’,’l’,’l’,’o’]
■ Creating lists of single characters or single digits:
■ L1=list(input(“Enter list elements:”))
■ E.g. l=list(input (“Enter list elements”))
■ Enter list elements:23457
■ >>>l
■ [‘2’,’3’,’4’,’5’,’7’]
Ways to create TUPLE
 Creating a tuple is similar to list creation, but here we need put a
number of expressions in parentheses().
 T=()
 The built-in tuple type object to create tuples from sequence as per
syntax as:
 T=tuple(<sequence>)
 >>> t1=tuple(“world”)
 >>> t1
 (‘w’,’o’,’r’,’l’,’d’)
 Creating tuples of single characters or single digits as:
 T1=tuple(input(“Enter tuple elements:”))
Ways to create DICTIONARY
■ To create dictionary there are many ways:
 By initializing a dictionary: in this method all the key: pairs of a
dictionary are written collectively ,separated by commas and
enclosed in curly braces.
 Employee={‘name’:’Ravi’,’salary’:’10000’,’age’,:’22’}
 To create a empty dictionary :
 Employees{}
 Also by :dict()
 We can create by “zip function”
>>> Employee=dict(zip((‘name’ ,’salary’ ,’age’),(‘Ravi’,’10000’,’22’)
>>> Employee
{‘salary’:’10000’,’age’:’22’,’name’:’Ravi’}
Convert list to tuple
■ Convert list to tuple:
List1=int(input(“Enter list:”))
Print(list1)
Tuple=tuple(list1)
Print(tuple)
E.g.: list=[3,4,6,8,9]
Print(list)
Tuple=tuple(list)
Print(tuple)
Output:
>>>
(3,4,6,8,9)
(3,4,6,8,9)
>>>
Convert tuple to list
 Convert tuple to list:
Tuple=int(input(“Enter tuple:”))
Print(tuple)
L=list(tuple)
Print(L)
 E.g., tuple=(4,8,9,0)
Print(tuple)
L=list(tuple)
Print(L)
 Output:
>>>
(‘4’,’8’,’9’,’0’)
(‘4’,’8’,’9’,0’)
>>>
Convert tuple to dictionary
■ Convert tuple to dictionary:
T=(input(“Enter tuple:”))
Print(t)
Dic=dic(t)
Print(dic)
■ E.g., t=((‘b’,2),(‘h’,3))
Print(t)
Dic=dic(t)
Print(dic)
■ Output:
>>>
((‘b’,2),(‘h’,3))
{‘b’:2,’h’:3}
>>>
Convert dictionary to tuple
■ Convert dictionary to tuple:
Dict=input(“Enter dictionary:”)
Print(dict)
Tuple=dict.items()
Print(tuple)
■ E.g., dict={‘v’:1,’a’:2,’n’:3}
Print(dict)
Tuple=dict.items()
Print(tuple)
■ Output:
>>>
{‘v’:1,’a’,:2,’n’:3}
Dict_items ([(‘v’,1),(‘a’,2),(‘n’,3)])
>>>
Convert list to dictionary
■ Convert list to dictionary:
Lst=[(‘s’,3),(‘g’,8)]
Print(lst)
Dict=dict(lst)
Print(dict)

Output:
[(‘s’,3),(‘g’,8)]
{‘s’:3,’g’:8}
Convert dictionary to list
■ Convert dictionary to list:
Dict=input(“Enter dictionary”)
Print(dict)
L=dict.items()
Print(l)
■ E.g., dict={‘a’:2,’b’:4,’c’:5}
Print(dict)
L=dict.items()
Print(l)
Output:
>>>
{‘a’:2,’b’:4,’c’:5}
[(‘a’,2),(‘b’,4),(‘c’,5)]
>>>

You might also like