Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
UNIT III
LIST, TUPLES, DICTIONARIES AND SETS
Introduction to list and tuples, dictionaries and sets.
3.1 Lists
Introduction
A list in Python is an ordered sequence of items. It is one of the most versatile data types in
Python, capable of holding a mixture of item types (integers, floats, strings, and even other
lists). Lists are mutable, meaning their content can be changed after they are created.
Characteristics
 Ordered: The items in a list appear in a specific order. This means that we can use an
index to access and refer to each item.
 Mutable: You can add, remove, or change items after a list's creation.
 Dynamic: Lists can grow or shrink in size as needed, providing flexibility in dynamic
data collection scenarios.
Common Operations
 Appending: Add an item to the end of the list with append().
 Inserting: Insert an item at a specified position with insert().
 Removing: Remove an item with remove() or pop().
 Slicing: Access subsets of the list with slicing [:].
 Iterating: Iterate over a list using a for-loop.
List
A list is suitable for managing ordered, changeable collections that can contain duplicate
elements. In civil engineering, a list can be used to store a series of measurement data, such as
temperatures recorded over a period at a construction site.
# Example: Temperatures (in Celsius) recorded at a construction site over a week
temperatures = [22, 24, 23, 24, 25, 26, 24]
print(temperatures)
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
3.2 Tuples
Introduction
A tuple is similar to a list in that it is an ordered sequence of items. However, tuples are
immutable, meaning once a tuple is created, its content cannot be changed.
Characteristics
 Ordered: The items have a defined order.
 Immutable: Once a tuple is created, you cannot change, add, or remove items.
 Fixed Size: Due to immutability, a tuple's size is fixed once it is created.
Common Uses
 Storing constant data: Useful for data that should not change throughout the execution
of a program.
 Function Arguments and Return Values: Often used to pass a collection of values.
Tuple
Tuples are ideal for storing ordered collections of items that cannot be changed. This is useful
for civil engineering data that should remain constant once defined, such as coordinates of a
landmark or fixed properties of materials.
# Example: GPS coordinates of a bridge (latitude, longitude)
bridge_coordinates = (35.6895, 139.6917)
print(bridge_coordinates)
# Example: Material properties (density, Young's modulus) of steel
steel_properties = (7850, 210e9
) # Density (kg/m^3), Young's modulus (Pa)
print(steel_properties)
3.3 Sets
Introduction
A set is an unordered collection of items where every element is unique (no duplicates) and
must be immutable (cannot be changed).
Characteristics
 Unordered: The items do not have a defined order.
 No Duplicates: Each element in a set is unique.
 Mutable: You can add or remove items from a set, but the items themselves must be
immutable.
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
Common Operations
 Adding items: Use add() to add an item to a set.
 Removing items: Use remove() or discard() to remove an item.
 Union, Intersection, Difference: Perform set operations to compare sets.
Set
Sets are useful for storing unique, unordered items, making them perfect for situations where
duplicates are not allowed and order does not matter. In civil engineering, a set can be used to
manage a collection of unique construction materials or tools needed for a project.
# Example: Unique construction materials required for a project
construction_materials = {"cement", "sand", "gravel", "steel", "concrete"}
print(construction_materials)
3.4 Dictionaries
Introduction
A dictionary in Python is an unordered collection of items. Each item is stored as a key-value
pair. This is a highly efficient data type for storing and retrieving data as its structure is
optimized for quick lookups.
Characteristics
 Unordered: The items do not have a defined order.
 Mutable: You can add, remove, or change items after the dictionary's creation.
 Key-Value Pairs: Data is stored as keys and values, where keys must be unique.
Common Operations
 Adding items: Directly assign a value to a new key.
 Updating items: Assign a new value to an existing key.
 Removing items: Use del or pop() to remove items.
 Accessing: Access values using their keys.
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
Dictionary
Dictionaries store data in key-value pairs, allowing for efficient data lookup and management.
This can be particularly useful in civil engineering for mapping properties, such as storing
information about various sections of a roadway or attributes of buildings within a project.
# Example: Storing load capacity (kN) of different sections of a bridge
bridge_load_capacity = { "section_1": 500, "section_2": 450, "section_3": 550 }
print(bridge_load_capacity)
# Example: Storing information about a building
building_info = {
"name": "Tower XYZ",
"height": 350, # in meters
"year_built": 2020,
"material": "reinforced concrete"
}
print(building_info)
These examples demonstrate how Python collections can be leveraged in civil engineering to
handle a wide range of data types and requirements, from simple lists of measurements to
complex mappings of structural properties and coordinates.
#Applications of List, Tuple, Sets and Dictionary in Civil Engineering
1. Lists: Dynamic Data Collection and Analysis
Application: Monitoring and analyzing environmental conditions at a construction site.
Civil engineers often monitor environmental conditions like temperature, humidity, and wind
speed to assess their impact on construction materials and work progress. Using lists, engineers
can dynamically collect, append, and analyze these data over time, enabling them to make
# Collecting daily average temperatures at a construction site over a month
temperatures = [22, 24, 23, 26, 25, 27, 28, 29, 30, 22, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
33, 34, 35, 36, 37, 38, 39, 40, 41]
# Analyzing temperature data to ensure materials like concrete are cured under optimal
conditions
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
2. Tuples: Immutable Data Representation
Application: Defining properties of construction materials.
Civil engineers need to define and use fixed properties of construction materials like steel and
concrete, such as density and Young's modulus, for structural analysis and design calculations.
Tuples, being immutable, ensure these critical properties remain unaltered throughout the
analysis process.
# Properties: Density (kg/m^3), Young's modulus (GPa), Compressive strength (MPa)
steel_properties = (7850, 210, None) # Steel does not have a compressive strength in the
traditional sense
concrete_properties = (2400, 25, 30)
# Using these properties in structural analysis calculations
3. Sets: Unique Item Management
Application: Managing construction site inventory.
In managing the inventory of tools and materials for a construction project, ensuring that items
are unique and that no duplicates are present in the inventory list is crucial. Sets automatically
enforce uniqueness, making them ideal for tracking inventory items.
# Inventory of unique tools required for a construction project
construction_tools = {"hammer", "drill", "saw", "screwdriver", "wrench", "level"}
# Easily adding and checking for tools without worrying about duplicates
4. Dictionaries: Structured Data Management
Application: Infrastructure asset management.
Civil engineers manage vast amounts of data related to infrastructure assets, such as bridges,
roads, and buildings. Dictionaries allow for structured and efficient management of such data,
associating unique identifiers or names with detailed information about each asset.
# Managing a database of bridges within a city's infrastructure
bridges_database = {
"Bridge_001": {"location": "River A", "length": 800, "construction_year": 1998,
"load_capacity": "50 tons"},
"Bridge_002": {"location": "River B", "length": 500, "construction_year": 2005,
"load_capacity": "40 tons"},
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
# Additional bridge entries
}
# Accessing and updating bridge information efficiently
These applications illustrate how the fundamental data structures in Python can be applied to
various aspects of civil engineering, from data collection and analysis to inventory management
and structured data handling of complex infrastructure projects.
#A comprehensive scenario in civil engineering that utilizes lists, tuples, sets, and
dictionaries all together. Our scenario will involve the construction of a residential
housing project.
Scenario: Residential Housing Project Management
You are managing a residential housing project that includes multiple buildings, and you need
to monitor construction progress, manage materials, and ensure all safety standards are met.
For this project, we'll track the materials needed, record the GPS locations of each building,
monitor daily temperatures for concrete curing, and keep a detailed log of each building's
specifications and progress.
1. List: Monitoring Daily Temperatures for Concrete Curing
For optimal concrete curing, temperature is a critical factor. You decide to keep a daily record
of temperatures for a month.
# Example: Temperatures recorded daily for a month at the construction site
daily_temperatures = [23, 25, 26, 24, 22, 27, 26, 25, 24, 23, 28, 29, 30, 22, 21, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37]
2. Tuple: Recording GPS Locations of Each Building
Each building within the project has a fixed GPS location. Since these coordinates are
immutable once construction starts, tuples are perfect for this.
# Example: GPS coordinates for buildings (latitude, longitude)
building_A_coords = (40.7128, -74.0060)
building_B_coords = (40.7129, -74.0059)
building_locations = {"A": building_A_coords, "B": building_B_coords}
3. Set: Managing Unique Construction Materials
To ensure there are no duplicate entries in the material orders and inventory, a set can be used
to manage the list of unique materials required.
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
# Example: Unique materials needed for the project
construction_materials = {"cement", "sand", "gravel", "steel rebar", "wood", "glass",
"insulation"}
4. Dictionary: Building Specifications and Progress Log
Each building in the project has specific details and progress updates that need to be managed
efficiently. Dictionaries are ideal for this structured data.
# Example: Detailed information and progress for each building
buildings_info = {
"Building_A": {
"coordinates": building_A_coords,
"foundation_completed": True,
"structure_completed": False,
"interior_completed": False,
"materials_used": {"cement", "sand", "steel rebar"},
"progress": 50 # Percentage
},
"Building_B": {
"coordinates": building_B_coords,
"foundation_completed": True,
"structure_completed": True,
"interior_completed": False,
"materials_used": {"cement", "sand", "steel rebar", "wood", "glass"},
"progress": 75 # Percentage
}
}
Prepared by Prof. Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon.
Integrating All Collections in a Unified Problem-Solving Approach:
In managing the residential housing project, you'll use:
- Lists to monitor and analyze temperature data for concrete curing.
- Tuples to securely record immutable data like the GPS locations of buildings.
- Sets to manage unique lists of materials, ensuring efficiency in procurement and inventory
management.
- Dictionaries to hold structured data about each building's specifications, progress, and the
materials used, allowing for easy access and updates.
This comprehensive approach enables efficient management of the construction project,
leveraging the strengths of each type of Python collection to handle different aspects of the
project.

Programming in Civil Engineering_UNIT 3_NOTES

  • 1.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. UNIT III LIST, TUPLES, DICTIONARIES AND SETS Introduction to list and tuples, dictionaries and sets. 3.1 Lists Introduction A list in Python is an ordered sequence of items. It is one of the most versatile data types in Python, capable of holding a mixture of item types (integers, floats, strings, and even other lists). Lists are mutable, meaning their content can be changed after they are created. Characteristics  Ordered: The items in a list appear in a specific order. This means that we can use an index to access and refer to each item.  Mutable: You can add, remove, or change items after a list's creation.  Dynamic: Lists can grow or shrink in size as needed, providing flexibility in dynamic data collection scenarios. Common Operations  Appending: Add an item to the end of the list with append().  Inserting: Insert an item at a specified position with insert().  Removing: Remove an item with remove() or pop().  Slicing: Access subsets of the list with slicing [:].  Iterating: Iterate over a list using a for-loop. List A list is suitable for managing ordered, changeable collections that can contain duplicate elements. In civil engineering, a list can be used to store a series of measurement data, such as temperatures recorded over a period at a construction site. # Example: Temperatures (in Celsius) recorded at a construction site over a week temperatures = [22, 24, 23, 24, 25, 26, 24] print(temperatures)
  • 2.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. 3.2 Tuples Introduction A tuple is similar to a list in that it is an ordered sequence of items. However, tuples are immutable, meaning once a tuple is created, its content cannot be changed. Characteristics  Ordered: The items have a defined order.  Immutable: Once a tuple is created, you cannot change, add, or remove items.  Fixed Size: Due to immutability, a tuple's size is fixed once it is created. Common Uses  Storing constant data: Useful for data that should not change throughout the execution of a program.  Function Arguments and Return Values: Often used to pass a collection of values. Tuple Tuples are ideal for storing ordered collections of items that cannot be changed. This is useful for civil engineering data that should remain constant once defined, such as coordinates of a landmark or fixed properties of materials. # Example: GPS coordinates of a bridge (latitude, longitude) bridge_coordinates = (35.6895, 139.6917) print(bridge_coordinates) # Example: Material properties (density, Young's modulus) of steel steel_properties = (7850, 210e9 ) # Density (kg/m^3), Young's modulus (Pa) print(steel_properties) 3.3 Sets Introduction A set is an unordered collection of items where every element is unique (no duplicates) and must be immutable (cannot be changed). Characteristics  Unordered: The items do not have a defined order.  No Duplicates: Each element in a set is unique.  Mutable: You can add or remove items from a set, but the items themselves must be immutable.
  • 3.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. Common Operations  Adding items: Use add() to add an item to a set.  Removing items: Use remove() or discard() to remove an item.  Union, Intersection, Difference: Perform set operations to compare sets. Set Sets are useful for storing unique, unordered items, making them perfect for situations where duplicates are not allowed and order does not matter. In civil engineering, a set can be used to manage a collection of unique construction materials or tools needed for a project. # Example: Unique construction materials required for a project construction_materials = {"cement", "sand", "gravel", "steel", "concrete"} print(construction_materials) 3.4 Dictionaries Introduction A dictionary in Python is an unordered collection of items. Each item is stored as a key-value pair. This is a highly efficient data type for storing and retrieving data as its structure is optimized for quick lookups. Characteristics  Unordered: The items do not have a defined order.  Mutable: You can add, remove, or change items after the dictionary's creation.  Key-Value Pairs: Data is stored as keys and values, where keys must be unique. Common Operations  Adding items: Directly assign a value to a new key.  Updating items: Assign a new value to an existing key.  Removing items: Use del or pop() to remove items.  Accessing: Access values using their keys.
  • 4.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. Dictionary Dictionaries store data in key-value pairs, allowing for efficient data lookup and management. This can be particularly useful in civil engineering for mapping properties, such as storing information about various sections of a roadway or attributes of buildings within a project. # Example: Storing load capacity (kN) of different sections of a bridge bridge_load_capacity = { "section_1": 500, "section_2": 450, "section_3": 550 } print(bridge_load_capacity) # Example: Storing information about a building building_info = { "name": "Tower XYZ", "height": 350, # in meters "year_built": 2020, "material": "reinforced concrete" } print(building_info) These examples demonstrate how Python collections can be leveraged in civil engineering to handle a wide range of data types and requirements, from simple lists of measurements to complex mappings of structural properties and coordinates. #Applications of List, Tuple, Sets and Dictionary in Civil Engineering 1. Lists: Dynamic Data Collection and Analysis Application: Monitoring and analyzing environmental conditions at a construction site. Civil engineers often monitor environmental conditions like temperature, humidity, and wind speed to assess their impact on construction materials and work progress. Using lists, engineers can dynamically collect, append, and analyze these data over time, enabling them to make # Collecting daily average temperatures at a construction site over a month temperatures = [22, 24, 23, 26, 25, 27, 28, 29, 30, 22, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41] # Analyzing temperature data to ensure materials like concrete are cured under optimal conditions
  • 5.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. 2. Tuples: Immutable Data Representation Application: Defining properties of construction materials. Civil engineers need to define and use fixed properties of construction materials like steel and concrete, such as density and Young's modulus, for structural analysis and design calculations. Tuples, being immutable, ensure these critical properties remain unaltered throughout the analysis process. # Properties: Density (kg/m^3), Young's modulus (GPa), Compressive strength (MPa) steel_properties = (7850, 210, None) # Steel does not have a compressive strength in the traditional sense concrete_properties = (2400, 25, 30) # Using these properties in structural analysis calculations 3. Sets: Unique Item Management Application: Managing construction site inventory. In managing the inventory of tools and materials for a construction project, ensuring that items are unique and that no duplicates are present in the inventory list is crucial. Sets automatically enforce uniqueness, making them ideal for tracking inventory items. # Inventory of unique tools required for a construction project construction_tools = {"hammer", "drill", "saw", "screwdriver", "wrench", "level"} # Easily adding and checking for tools without worrying about duplicates 4. Dictionaries: Structured Data Management Application: Infrastructure asset management. Civil engineers manage vast amounts of data related to infrastructure assets, such as bridges, roads, and buildings. Dictionaries allow for structured and efficient management of such data, associating unique identifiers or names with detailed information about each asset. # Managing a database of bridges within a city's infrastructure bridges_database = { "Bridge_001": {"location": "River A", "length": 800, "construction_year": 1998, "load_capacity": "50 tons"}, "Bridge_002": {"location": "River B", "length": 500, "construction_year": 2005, "load_capacity": "40 tons"},
  • 6.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. # Additional bridge entries } # Accessing and updating bridge information efficiently These applications illustrate how the fundamental data structures in Python can be applied to various aspects of civil engineering, from data collection and analysis to inventory management and structured data handling of complex infrastructure projects. #A comprehensive scenario in civil engineering that utilizes lists, tuples, sets, and dictionaries all together. Our scenario will involve the construction of a residential housing project. Scenario: Residential Housing Project Management You are managing a residential housing project that includes multiple buildings, and you need to monitor construction progress, manage materials, and ensure all safety standards are met. For this project, we'll track the materials needed, record the GPS locations of each building, monitor daily temperatures for concrete curing, and keep a detailed log of each building's specifications and progress. 1. List: Monitoring Daily Temperatures for Concrete Curing For optimal concrete curing, temperature is a critical factor. You decide to keep a daily record of temperatures for a month. # Example: Temperatures recorded daily for a month at the construction site daily_temperatures = [23, 25, 26, 24, 22, 27, 26, 25, 24, 23, 28, 29, 30, 22, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37] 2. Tuple: Recording GPS Locations of Each Building Each building within the project has a fixed GPS location. Since these coordinates are immutable once construction starts, tuples are perfect for this. # Example: GPS coordinates for buildings (latitude, longitude) building_A_coords = (40.7128, -74.0060) building_B_coords = (40.7129, -74.0059) building_locations = {"A": building_A_coords, "B": building_B_coords} 3. Set: Managing Unique Construction Materials To ensure there are no duplicate entries in the material orders and inventory, a set can be used to manage the list of unique materials required.
  • 7.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. # Example: Unique materials needed for the project construction_materials = {"cement", "sand", "gravel", "steel rebar", "wood", "glass", "insulation"} 4. Dictionary: Building Specifications and Progress Log Each building in the project has specific details and progress updates that need to be managed efficiently. Dictionaries are ideal for this structured data. # Example: Detailed information and progress for each building buildings_info = { "Building_A": { "coordinates": building_A_coords, "foundation_completed": True, "structure_completed": False, "interior_completed": False, "materials_used": {"cement", "sand", "steel rebar"}, "progress": 50 # Percentage }, "Building_B": { "coordinates": building_B_coords, "foundation_completed": True, "structure_completed": True, "interior_completed": False, "materials_used": {"cement", "sand", "steel rebar", "wood", "glass"}, "progress": 75 # Percentage } }
  • 8.
    Prepared by Prof.Rushikesh Kolhe, Asst. Professor, Department of Civil Engineering, SCOE, Kopargaon. Integrating All Collections in a Unified Problem-Solving Approach: In managing the residential housing project, you'll use: - Lists to monitor and analyze temperature data for concrete curing. - Tuples to securely record immutable data like the GPS locations of buildings. - Sets to manage unique lists of materials, ensuring efficiency in procurement and inventory management. - Dictionaries to hold structured data about each building's specifications, progress, and the materials used, allowing for easy access and updates. This comprehensive approach enables efficient management of the construction project, leveraging the strengths of each type of Python collection to handle different aspects of the project.