Collections in Python

Collections in Python

There are four collection data types in the Python language:

  • List: A list is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
  • Tuple: A tuple is a sequence of unchangeable Python objects.
  • Set: A Set is an unordered collection data type that is iterable, changeable, and has no duplicate items. 
  • Dictionary: Python dictionary is an unordered collection of elements. A dictionary has a key and value combination.

# Using Lists in python
m=[‘Apple’,’Orange’,’Cherry’]
print(“Given list:”, m)

#Reverse list
m.reverse()
print(“Reverse List:”,m)

# Appending list
m.append(‘Banana’)
print(“Appending list:”,m)

# Copying list from one to another
n=m.copy()
print(“Copying list from one to another:”,n)

#inserting elements at index position
m.insert(1,’Kiwi’)
print(“Adding new value:”,m)

# Removing elements from specified index location
m.pop(1)
print(“Removing:”,m)

#sorted values
m.sort()
print(“sorting”,m)

# range of values
print(m[1:3]) # from second

# Looping in list
for x in m:
print(x)

# if statement in List
if ‘Apple’ in m:
print(“Yes! Apple is there”)
else:
print(“Sorry! Apple is not in the list”)

# Length of the list
item=[‘PC’, ‘Laptop’,’LED’,’LCD’]
print(item)
print(“Total no of items in list:”,len(item))

OUTPUT:
Given list: [‘Apple’, ‘Orange’, ‘Cherry’]
Reverse List: [‘Cherry’, ‘Orange’, ‘Apple’]
Appending list: [‘Cherry’, ‘Orange’, ‘Apple’, ‘Banana’]
Copying list from one to another: [‘Cherry’, ‘Orange’, ‘Apple’, ‘Banana’]
Adding new value: [‘Cherry’, ‘Kiwi’, ‘Orange’, ‘Apple’, ‘Banana’]
Removing: [‘Cherry’, ‘Orange’, ‘Apple’, ‘Banana’]
sorting [‘Apple’, ‘Banana’, ‘Cherry’, ‘Orange’]
[‘Banana’, ‘Cherry’]
Apple
Banana
Cherry
Orange
Yes! Apple is there
[‘PC’, ‘Laptop’, ‘LED’, ‘LCD’]
Total no of items in list: 4

#Tuples: A tuple is a group of items which is ordered and unchangeable.
# Tuples are writen using round brackets in python

#Creating Tuple
m=(“Red”, “Green”, “Blue”, “Yellow”, “Pink”)
print(m)

# accessing tuples value using index
print(m[1])

# using count method in tuples, counts no repetition of particular value in tuple
tuples=(1,2,3,9,3,5,6,5,2)
x=tuples.count(2)
print(x)

# Returns index of given value
x=tuples.index(9)
print(x)

OUTPUT:
(‘Red’, ‘Green’, ‘Blue’, ‘Yellow’, ‘Pink’)
Green
2
3

# A set is a collection of items which is unordered and unindexed.
# sets are written with curly brackets in python.

# creating sets in python
mset={“Java”, “Python”, “CPP”}
print(mset)

# accessing set values using loop
for x in mset:
print(x)

#adding values to sets
mset.add(“PHP”)
print(mset)

# Removing specified items from sets
mset.discard(“CPP”)
print(mset)

OUTPUT:
{‘CPP’, ‘Python’, ‘Java’}
CPP
Python
Java
{‘CPP’, ‘Python’, ‘PHP’, ‘Java’}
{‘Python’, ‘PHP’, ‘Java’}

# A dictionary is a collection or group of items and are unordered,
# and changeable and indexed.
#In Python dictionaries are written with curly brackets, and they have keys and values.

#creating dictionary in python
dict={
“OS”: “Windows”,
“Processor”: “Corei3”,
“Year of Launch”:2010}

print(dict)

# Accessing values based on their key
x=dict[“OS”]
print(x)

x=dict[“Processor”]
print(x)

x=dict[“Year of Launch”]
print(x)

# Accessing all values from dictionary
y=dict.values()
print(y)

OUTPUT:
{‘Year of Launch’: 2010, ‘OS’: ‘Windows’, ‘Processor’: ‘Corei3’}
Windows
Corei3
2010
dict_values([2010, ‘Windows’, ‘Corei3’])

Share
Share
Scroll to Top