Get Started

Compute degree distribution

The number of neighbors that a node has is called its "degree", and it's possible to compute the degree distribution across the entire graph. In this exercise, your job is to compute the degree distribution across T.

This is a part of the course

“Introduction to Network Analysis in Python”

View Course

Exercise instructions

  • Use a list comprehension along with the .neighbors(n) method to get the degree of every node. The result should be a list of integers.
    • Use n as your iterator variable.
    • The output expression of your list comprehension should be the number of neighbors that node n has - that is, its degree. Use the len() and list() functions together with the .neighbors() method to compute this.
    • The iterable in your list comprehension is all the nodes in T, accessed using the .nodes() method.
  • Print the degrees.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Compute the degree of every node: degrees
degrees = [____]

# Print the degrees
Edit and Run Code

This exercise is part of the course

Introduction to Network Analysis in Python

IntermediateSkill Level
4.6+
7 reviews

This course will equip you with the skills to analyze, visualize, and make sense of networks using the NetworkX library.

You'll learn about ways to identify nodes that are important in a network. In doing so, you'll be introduced to more advanced concepts in network analysis as well as the basics of path-finding algorithms. The chapter concludes with a deep dive into the Twitter network dataset which will reinforce the concepts you've learned, such as degree centrality and betweenness centrality.

Exercise 1: Degree centralityExercise 2: Compute number of neighbors for each nodeExercise 3: Compute degree distribution
Exercise 4: Degree centrality distributionExercise 5: Graph algorithmsExercise 6: Shortest Path IExercise 7: Shortest Path IIExercise 8: Shortest Path IIIExercise 9: Betweenness centralityExercise 10: NetworkX betweenness centrality on a social networkExercise 11: Deep dive - Twitter networkExercise 12: Deep dive - Twitter network part II

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free