0% found this document useful (0 votes)
12 views11 pages

Adding Tropical Fruits to Lists

This document provides examples of using various Python list, tuple, set, and dictionary operations including accessing, modifying, looping through, joining, copying and sorting the different data structures. It demonstrates common list methods like append(), remove(), sort(); tuple operations like unpacking and joining; set operations like union(), intersection() and symmetric difference; and dictionary methods for accessing, modifying, adding, removing and looping through key-value pairs.

Uploaded by

Vijay Arapath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Adding Tropical Fruits to Lists

This document provides examples of using various Python list, tuple, set, and dictionary operations including accessing, modifying, looping through, joining, copying and sorting the different data structures. It demonstrates common list methods like append(), remove(), sort(); tuple operations like unpacking and joining; set operations like union(), intersection() and symmetric difference; and dictionary methods for accessing, modifying, adding, removing and looping through key-value pairs.

Uploaded by

Vijay Arapath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Access List Item

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

Chance List Item

thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)

Add List Item

thislist = ["apple", "banana", "cherry"]
[Link]("orange")
print(thislist)

remove list items

thislist = ["apple", "banana", "cherry"]
[Link]("banana")
print(thislist)

Loop List

thislist = ["apple", "banana", "cherry"]
for x in thislist:
  print(x)

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
  if "a" in x:
    [Link](x)

print(newlist)

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]

newlist = [x for x in fruits if "a" in x]

print(newlist)

sort()

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
[Link]()
print(thislist)

thislist = [100, 50, 65, 82, 23]
[Link]()
print(thislist)

thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
[Link](reverse = True)
print(thislist)

copy

thislist = ["apple", "banana", "cherry"]
mylist = [Link]()
print(mylist)

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)
join

list1 = ["a", "b", "c"]
list2 = [1, 2, 3]

list3 = list1 + list2


print(list3)

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]

for x in list2:
  [Link](x)

print(list1)

Tuples

Access tuples

thistuple = ("apple", "banana", "cherry")
print(thistuple[1])

update tuples

x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)

print(x)

thistuple = ("apple", "banana", "cherry")
[Link]("orange") # This will raise an error
print(thistuple)
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
[Link]("orange")
thistuple = tuple(y)

Unpack Tuples

fruits = ("apple", "banana", "cherry")

(green, yellow, red) = fruits

print(green)
print(yellow)
print(red)

loop

thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)

thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
  print(thistuple[i])

join tuples

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2


print(tuple3)

fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)

Set

Access Set item

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

for x in thisset:
  print(x)

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

print("banana" in thisset)

ADD

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

[Link]("orange")

print(thisset)

thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

[Link](tropical)

print(thisset)

REMOVE

thisset = {"apple", "banana", "cherry"}
[Link]("banana")

print(thisset)

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

[Link]("banana")

print(thisset)

loop

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

for x in thisset:
  print(x)

join

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

set3 = [Link](set2)
print(set3)

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}

[Link](set2)
print(set1)

Keep the items that exist in both set x, and set y:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.intersection_update(y)
print(x)

Return a set that contains the items that exist in both set x, and set y:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = [Link](y)

print(z)

Keep the items that are not present in both sets:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.symmetric_difference_update(y)

print(x)

Return a set that contains all items from both sets, except items that are
present in both:

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

z = x.symmetric_difference(y)

print(z)
Dictionary

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

Access Item

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict["model"]

Change Item

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018

Add Item

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
print(thisdict)
remove Items

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
[Link]("model")
print(thisdict)

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
[Link]()
print(thisdict)

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict["model"]
print(thisdict)

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer
exists.

Loop

for x in thisdict:
  print(thisdict[x])
for x in [Link]():
  print(x)

for x in [Link]():
  print(x)

for x, y in [Link]():
  print(x, y)

copy

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = [Link]()
print(mydict)

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
mydict = dict(thisdict)
print(mydict)

nested Dictnary

myfamily = {
  "child1" : {
    "name" : "Emil",
    "year" : 2004
  },
  "child2" : {
    "name" : "Tobias",
    "year" : 2007
  },
  "child3" : {
    "name" : "Linus",
    "year" : 2011
  }
}

child1 = {
  "name" : "Emil",
  "year" : 2004
}
child2 = {
  "name" : "Tobias",
  "year" : 2007
}
child3 = {
  "name" : "Linus",
  "year" : 2011
}

myfamily = {
  "child1" : child1,
  "child2" : child2,
  "child3" : child3
}

You might also like