How Instagram “People You May Know” Works Using Python: Build a Recommendation System Step-by-Step
Recommendation Systems are one of the most powerful technologies used by modern social media platforms like Instagram, Facebook, YouTube, LinkedIn, Netflix, and Amazon.
Every day, these platforms recommend:
- Friends
- Pages
- Videos
- Products
- Communities
- Posts
But have you ever wondered how these systems actually work?
In this tutorial, you will learn how to build a simple Instagram-style recommendation system using Python and JSON data.
You will learn:
- How recommendation systems think
- How Instagram suggests pages
- How “People You May Know” works
- How to build user lookups
- How to recommend pages using friend relationships
- How real-world Data Science systems work
This is one of the most important concepts in Data Science, AI, Analytics, and Machine Learning.
What is a Recommendation System?
A Recommendation System is an intelligent system that suggests relevant content to users.
Examples:
- Instagram suggests creators
- YouTube recommends videos
- Netflix suggests movies
- Amazon recommends products
- LinkedIn suggests connections
These systems analyze:
- user behavior
- relationships
- interests
- engagement
- interaction patterns
to predict what users may like.
Real-World Example
Suppose:
- Amit likes Python
- Amit’s friends like AI and Data Science
- Amit does not follow those pages yet
The system thinks:
“Amit may also like AI and Data Science”
This becomes a recommendation.
This is exactly how social media recommendation systems work.
Step 1 — Create the Dataset
We will use a JSON-style dataset.
data = {
"users": [
{
"id": 1,
"name": "Amit",
"friends": [2,3],
"liked_pages": [101]
},
{
"id": 2,
"name": "Priya",
"friends": [1,4],
"liked_pages": [101,102]
},
{
"id": 3,
"name": "Rahul",
"friends": [1],
"liked_pages": [103]
},
{
"id": 4,
"name": "Sara",
"friends": [2],
"liked_pages": [101,104]
}
],
"pages": [
{"id":101,"name":"Python"},
{"id":102,"name":"Data Science"},
{"id":103,"name":"AI"},
{"id":104,"name":"Web Dev"}
]
}
Understanding the Dataset
Users Table
| User | Friends | Liked Pages |
| Amit | Priya, Rahul | Python |
| Priya | Amit, Sara | Python, Data Science |
| Rahul | Amit | AI |
| Sara | Priya | Python, Web Dev |
Pages Table
| Page ID | Page Name |
| 101 | Python |
| 102 | Data Science |
| 103 | AI |
| 104 | Web Dev |
How Recommendation Logic Works
The system asks:
“What do Amit’s friends like that Amit does not already like?”
This is the core logic behind recommendation systems.
Step 2 — Create User Lookup Dictionary
Professional systems avoid searching repeatedly.
Instead, they create fast lookup systems.
Create User Lookup
user_lookup = {}
Fill the Dictionary
for user in data['users']:
user_lookup[user['id']] = user
Result
{
1: Amit data,
2: Priya data,
3: Rahul data,
4: Sara data
}
Why Lookup Dictionaries Matter
Without lookup:
- slow searching
- repeated loops
- inefficient systems
With lookup:
- instant access
- faster recommendation engines
- scalable architecture
Real platforms use optimized lookup systems internally.
Step 3 — Create Page Lookup Dictionary
We also need page names from page IDs.
Create Page Lookup
page_lookup = {}
Fill the Dictionary
for page in data['pages']:
page_lookup[page['id']] = page['name']
Result
{
101: "Python",
102: "Data Science",
103: "AI",
104: "Web Dev"
}
Step 4 — Select Current User
Suppose Amit opens Instagram.
We want to generate recommendations for Amit.
Get Amit’s Data
current_user = user_lookup[1]
Result
{
"id":1,
"name":"Amit",
"friends":[2,3],
"liked_pages":[101]
}
Understanding the Goal
Amit already likes:
- Python
The system should NOT recommend:
- Python again
Instead, it should recommend:
- new pages
Step 5 — Create Recommendation List
Empty Recommendation List
recommended_pages = []
Step 6 — Loop Through Friends
Now we inspect what Amit’s friends like.
for friend_id in current_user['friends']:
First Iteration
friend_id = 2
This means:
- Priya
Get Friend Data
friend = user_lookup[friend_id]
Result
{
"id":2,
"name":"Priya",
"liked_pages":[101,102]
}
Step 7 — Check Friend’s Pages
Now inspect pages liked by Priya.
for page_id in friend['liked_pages']:
Priya Likes
[101,102]
Step 8 — Apply Recommendation Logic
We only recommend pages Amit does NOT already like.
if page_id not in current_user['liked_pages']:
Why This Matters
Amit already likes:
- Python
So recommending Python again is unnecessary.
Instead:
- recommend Data Science
Add Recommendation
recommended_pages.append(page_id)
Continue For All Friends
The same logic runs for:
- Rahul
- Sara
- future users
Final Recommendation Result
Eventually:
[102,103]
This means:
- Data Science
- AI
Step 9 — Remove Duplicate Recommendations
Sometimes multiple friends like the same page.
We remove duplicates using set().
recommended_pages = list(set(recommended_pages))
Step 10 — Convert Page IDs into Names
Now show actual page names.
for page_id in recommended_pages:
print(page_lookup[page_id])
Final Output
Data Science AI
Complete Recommendation System Code
# USER LOOKUP
user_lookup = {}
for user in data['users']:
user_lookup[user['id']] = user
# PAGE LOOKUP
page_lookup = {}
for page in data['pages']:
page_lookup[page['id']] = page['name']
# CURRENT USER
current_user = user_lookup[1]
# RECOMMENDATION SYSTEM
recommended_pages = []
for friend_id in current_user['friends']:
friend = user_lookup[friend_id]
for page_id in friend['liked_pages']:
if page_id not in current_user['liked_pages']:
recommended_pages.append(page_id)
# REMOVE DUPLICATES
recommended_pages = list(set(recommended_pages))
# SHOW RECOMMENDATIONS
print("Recommended Pages:\n")
for page_id in recommended_pages:
print(page_lookup[page_id])
How Instagram Actually Uses Similar Logic
Instagram recommendation systems are much more advanced.
They analyze:
- Likes
- Comments
- Shares
- Watch Time
- Saves
- Friend Networks
- Similar Interests
- User Engagement
- Search History
But the core thinking is similar:
- Find patterns
- Predict interests
- Recommend relevant content
Real-World Recommendation Examples
| Platform | Recommendation Type |
| Suggested creators | |
| People You May Know | |
| Suggested connections | |
| YouTube | Recommended videos |
| Netflix | Recommended movies |
| Amazon | Suggested products |
Important Data Science Concepts You Learned
| Concept | Importance |
| Lookup Dictionaries | Fast access systems |
| Relationship Mapping | Social graph understanding |
| Recommendation Logic | Predicting user interest |
| Deduplication | Remove repeated results |
| JSON Processing | Structured data handling |
| Analytics Thinking | Real-world AI mindset |
Real Data Science Thinking
Weak programmers:
- Learn syntax only
Strong Data Scientists:
- Think in systems
- Understand relationships
- Build recommendation logic
- Design intelligent workflows
This is how billion-dollar applications work.
Next-Level Features You Can Build
After this project, you can build:
People You May Know
Suggest users with:
- mutual friends
- common interests
Trending Page System
Recommend:
- most liked pages
- most active communities
Interest Score Engine
Example:
- 5 friends like AI
- AI gets higher recommendation priority
AI Feed Ranking
Rank posts using:
- engagement
- likes
- comments
- watch time
Final Thoughts
Recommendation Systems are one of the most powerful applications of Data Science and Artificial Intelligence.
Even a simple Python-based recommendation engine teaches:
- relationship mapping
- structured thinking
- analytics logic
- scalable architecture
- user behavior prediction
If you master concepts like these, your Data Science thinking becomes much stronger than simply learning syntax.





