Difference between list and dictionary in C# vs Python

Lists

Lists, as the name implies, store a list of data items with different data types. Lists offer some interesting functions/methods to store, retrieve and process data and can be easily resized.

C# code

In C#, a list is an object of the  List<T> class which is defined in the namespace System.Collections.Generic. The data type of a list can be anything including built-in and user defined object types. Moreover, to store different types of data in a single list, we can use a list of objects  List<object> as we do in the example above. All items in such a list, irrespective of their native type (primitive or object), are type cast to object type. The new keyword, as we’ve previously used with built-in objects such as tuples as well as with user defined objects, allocates memory on the heap for our new object.  The list can be initialized using curly braces containing all the objects separated by commas. The add() method is used to add new items to the list.

In Python, a list is declared and initialized simply using square brackets. Since all data in Python is of type object, any list in Python is a list of objects. The append() method is used to add new items to the list.

Python code

 

Frequently used list methods

Remove items from list:

C#

Python

Deletes the item at index 4. The items from index 5 to the last index are shifted one index down.



Deletes the item “Monster”.



Removes all items from index 1 to 4.

Length of a list:



Sort and reverse:



Insert an item at specific index:

The  append() (or in C#,  add()) method simply adds the item to the end of the list. To add an item to a specific index, we can the  insert() function.



Get a sublist:



Gets objects in my_list from index 1 to 3 and assigns them to list  new_list.

Combine lists



Dictionaries

Dictionaries are also used to store collection of data items. Each data item (called value) in a dictionary must also be assigned a key. The key can then be used to access the value as opposed to the use of indices in lists. Therefore, dictionary is an un-ordered collection of key, value pairs.

C# code

Python code

In the example above, we create a dictionary with student names as the keys and their score as the values. The score of a student can then be retrieved using the student’s name. A dictionary in C# is declared using the keyword  Dicitonary<,>, with the data types for keys and values in the angled brackets.

Lets develop a more useful program using dictionaries and lists. Suppose we are building a data base of a company’s employees. The database needs to store the employees name, age, gender and their office number. If an employee’s details are in the database, we can access them using the employee’s name.

C# code

 

Python code

The dictionary  employee_dict uses the employee names as keys while the values are lists of objects containing all the employee data. The program checks if the provided input is a key in the dictionary using ContainsKey()  function in C# and the  in keyword in Python. The expression  in_name in employee_dict in Python returns true if  in_name is one of the keys in the dictionary. If the input key is found, the program prints the employee’s entire data.

Frequently used dictionary methods

Remove a key value pair

C#

Python

Number of entries

Clear dictionary

Iterate over lists and dictionaries

To iterate over all the items in a collection such as a list or a dictionary, specializations of the for loop are used together with the  in keyword. In the following example, we take a list of student scores in Mathematics and then iterate over the list to calculate their corresponding grades.

C# code

The first  foreach loop combined with the  in keyword iterates over the entire list  scores_maths. At each iteration, the current score in the list is stored in a new float variable  score. We then compare the value of  score with the grading thresholds and add the corresponding grade to the list  grades_maths. The list  grades_maths is a list of type  char which is another primitive data type in C# used to store a single character.

The second  foreach loop in our example iterates over the list  grades_maths. The current item is stored in a character variable grade and it is displayed along with the corresponding score at the output.

score_grades_output

Output: For each loop

In Python, we still use the keyword  for together with  in to iterate over a collection. Similar to C#, the current item is stored in a variable that can be used to test conditions or simply display the contents of the list one by one.

Python code

 

< Inheritance and static members               Pass by value, pass by reference >

You may also like: