This content originally appeared on DEV Community and was authored by Ramya .C
Mastering Python Lists & Dictionaries
Today marks the 7th day of my Data Analytics learning journey. I focused on Python’s core data structures like Lists and Dictionaries. Below is a quick breakdown of what I learned with definitions and example programs for each concept:
Python Lists
What is a List?
A list is a collection of items that are ordered, mutable, and can contain duplicates.
Homogeneous & Heterogeneous Lists:
- Homogeneous list – All elements are of the same type
numbers = [1, 2, 3, 4]
- Heterogeneous list – Elements of different types
mixed = [1, "Hello", 3.14, True]
Common List Operations:
1. Add Elements:
fruits = ["apple", "banana"]
fruits.append("cherry") # ['apple', 'banana', 'cherry']
fruits.insert(1, "mango") # ['apple', 'mango', 'banana', 'cherry']
2. Update Elements:
fruits[0] = "grape" # ['grape', 'mango', 'banana', 'cherry']
3. Remove Elements:
fruits.remove("banana") # ['grape', 'mango', 'cherry']
fruits.pop() # removes last element
4. Looping:
for fruit in fruits:
print(fruit)
5. Nested List:
nested = [[1, 2], [3, 4]]
print(nested[1][0]) # Output: 3
6. Slicing:
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4]) # [20, 30, 40]
7. Reverse:
numbers.reverse()
print(numbers) # [50, 40, 30, 20, 10]
Dictionary
What is a Dictionary?
A dictionary is a key-value pair data structure.
Example:
student = {"name": "Ramya", "age": 21, "course": "Data Analytics"}
print(student["name"]) # Output: Ramya
Today’s Tasks
1. Find Type of Variable:
x = 5
print(type(x)) # <class 'int'>
2. Find Range:
for i in range(1, 6):
print(i) # 1 to 5
3. Step in Range:
for i in range(0, 10, 2):
print(i) # 0, 2, 4, 6, 8
4. Push (Add element to list):
stack = []
stack.append(10)
stack.append(20)
print(stack) # [10, 20]
5. Reverse List:
nums = [1, 2, 3, 4]
print(list(reversed(nums))) # [4, 3, 2, 1]
6. Find Elements Less Than 5:
numbers = [1, 6, 3, 7, 4]
less_than_5 = [n for n in numbers if n < 5]
print(less_than_5) # [1, 3, 4]
7. List Comprehension:
squares = [x**2 for x in range(1, 6)]
print(squares) # [1, 4, 9, 16, 25]
Hashing in Python
What is Hashing?
Hashing is converting a value into a fixed-size integer using a hash function.
Example:
print(hash("Ramya")) # Outputs an integer (hash value)
Today I got hands-on experience with:
- Lists (add, update, delete, loop, slice, reverse)
- Dictionaries
- Concepts like
type()
,range()
,step
,push
,list comprehension
, andhashing
I’m one step closer to becoming a Data Analyst!
This content originally appeared on DEV Community and was authored by Ramya .C