Programmer Coding

Data Structures in Python

     1. Lists

     2. Tuples

     3. Dictionary

     4. Sets

 

  • Lists

A list contains collection of any records kind: strings, ints, different lists. The matters inside a listing are generically called “elements”. not like strings, lists are “mutable” – they may be modified

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh","Satyam Keshri","Mausam Mishra", "Jitendra Shrama", "Ankit Jha","Birendra Yadev", "Yathartha Gaurav"]
print("Best Friend : ",friends)

Output

Best Friend : [‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Satyam Keshri’, ‘Mausam Mishra’, ‘Jitendra Shrama’, ‘Ankit Jha’, ‘Birendra Yadev’, ‘Yathartha Gaurav’]

Lists Methods

  • append()
  • extend()
  • insert()
  • remove()
  • Pop()
  • index()
  • count()
  • sort()
  • reverse()

 

append()

The append() method is used to add an element to the end of a list.

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra"]
# Using append() to add a new friend
friends.append("Vikas Anand")
print(friends)

Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Mausam Mishra’, ‘Vikas Anand’]

extend()

The extend() method is one of the three ways to add elements to a list; the other two being append() and insert()

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra"]
# List of new friends to be added
new_friends = ["Akash Raz", "Ayushi"]
# Using extend() to add new friends
friends.extend(new_friends)
print(friends)

Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Mausam Mishra’, ‘Akash Raz’, ‘Ayushi’]

insert()

Just like append(), the insert() method is used to add stuff to a list. However, it isn’t limited only to the end of the list — it can add an element at any given position.

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra"]
# Using insert() to add a new friend at index 2
friends.insert(2, "Jitendra Sharma")
friends.insert(3, "Akash Raz")
print(friends)

Output

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Jitendra Sharma’, ‘Akash Raz’, ‘Vishnu Singh’, ‘Mausam Mishra’]

Pop()

The pop() method is used to remove an element at a given position, from a list, and return it.

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Satyam Keshri", "Jitendra Sharma", "Mausam Mishra"]
# Using pop() to remove and return the friend at index 1
removed_friend = friends.pop(2)
print(removed_friend)  # Output: Shubham Kashyap
print(friends)

Output

Vishnu Singh # removed

[‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Satyam Keshri’, ‘Jitendra Sharma’, ‘Mausam Mishra’]

index()

Returns the first appearance of a particular value.

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Mausam Mishra", "Jitendra Sharma", "Akash Raz", "Vikas Anand", "Ayushi"]
# Using index() to find the index of a friend
index = friends.index("Mausam Mishra")
print("Your index Value is : ",index)

Output

Your index Value is :  3

count()

Returns the number of elements with the required value.

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Vishnu Singh", "Mausam Mishra"]
# Using count() to count the occurrences of a friend
count = friends.count("Vishnu Singh")
print(count)

Output

2

sort()

Example
# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi","Satyam Keshri","Mausam Mishra", "Jitendra Shrama", "Ankit Jha","Birendra Yadev", "Yathartha Gaurav"]
# Using sort() to sort the list in alphabetical order
friends.sort()
print(friends)

Output

[‘Ankit Jha’, ‘Ayushi’, ‘Birendra Yadev’, ‘Jitendra Shrama’, ‘Mausam Mishra’, ‘Ramkrishna Kumar’, ‘Satyam Keshri’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Yathartha Gaurav’]

Sorts the list in ascending order.

reverse()

Reverses the order of the list

Example

# Original list
friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi","Satyam Keshri","Mausam Mishra", "Jitendra Shrama", "Ankit Jha","Birendra Yadev", "Yathartha Gaurav"]
# Using reverse() to reverse the list
friends.reverse()
print(friends)

Output

[‘Yathartha Gaurav’, ‘Birendra Yadev’, ‘Ankit Jha’, ‘Jitendra Shrama’, ‘Mausam Mishra’, ‘Satyam Keshri’, ‘Ayushi’, ‘Vishnu Singh’, ‘Shubham Kashyap’, ‘Ramkrishna Kumar’]

  • Tuples

A separate set of objects is called a Python triple. The order, decision, and repetition of tuples are similar to programs, but unlike programs, tuples are immutable.

Features of Python Tuple

  • Tuples are an immutable data type, meaning their elements cannot be changed after they are generated.
  • Each element in a tuple has a specific order that will never change because tuples are ordered sequences.

Forming a Tuple:

  • Forming a tuple in Python is straightforward. You can create a tuple by enclosing comma-separated values within parentheses (). Here’s how you can form a tuple:

# Forming a tuple with comma-separated values

my_tuple = (1, 2, 3, 4, 5)
  • You can also form a tuple without parentheses, but it’s generally a good practice to include them for clarity and to avoid ambiguity:

# Forming a tuple without parentheses (also valid but less clear)

my_tuple = 1, 2, 3, 4, 5
  • Additionally, you can create an empty tuple using empty parentheses:

# Forming an empty tuple

empty_tuple = ()

Slicing

Tuple slicing is a common practice in Python and is also the best way for programmers to solve practical problems. See tuples in Python. Split a bundle to access its different elements. One idea is to use the colon as a simple slicing operator (:).

We can use the forward slash operator (:) to access multiple tuple elements.

Example

# Define a tuple
my_tuple = (1, 2, 3, 4, 5)
# Perform tuple slicing
subset = my_tuple[1:4]  # Extract elements from index 1 to index 3 (exclusive)
# Print the subset
print("Subset of the tuple:", subset)

Output

Subset of the tuple: (2, 3, 4)

Tuple Methods

  • Count() Method
  • Index() Method

Count() Method

The times the predetermined component happens in the Tuple is returned by the count () capability of the Tuple.

Example

# Define a tuple
my_tuple = (1, 2, 3, 4, 2, 2, 5, 2)
# Count the number of occurrences of the element 2
count = my_tuple.count(2)
# Print the result
print("Number of occurrences of 2:", count)

Output

Number of occurrences of 2: 4

Index() Method

The Index() function returns the first instance of the requested element from the Tuple.

  • The thing that must be looked for.
  • Start: (Optional) the index that is used to begin the final (optional) search: The most recent index from which the search is carried out
  • Index Method

Example

# Define a tuple
my_tuple = (10, 20, 30, 20, 40)
# Find the index of the first occurrence of the element 20
index = my_tuple.index(20)
# Print the result
print("Index of the first occurrence of 20:", index)

Output

Index of the first occurrence of 20: 1

Dictionary Methods:

  • keys()
  • values()
  • items()
  • get()
  • update()
  • copy()

keys():

Returns a view of all keys in the dictionary.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
friends_dict = dict.fromkeys(friends,0)
print(friends_dict)

Output

{‘Ramkrishna Kumar’: 0, ‘Shubham Kashyap’: 0, ‘Vishnu Singh’: 0, ‘Ayushi’: 0, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 0, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0}

values():

Returns a view of all values in the dictionary.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
# Create a dictionary with default values set to 0
friends_dict = dict.fromkeys(friends, 0)
# Get a view of all values in the dictionary
values_view = friends_dict.values()
# Print the values view
print(values_view)

Output

dict_values([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

items():

Returns a view of all key-value pairs in the dictionary as tuples.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
# Create a dictionary with default values set to 0
friends_dict = dict.fromkeys(friends, 0)
# Get a view of all key-value pairs
items_view = friends_dict.items()
# Print the items view
print(items_view)

Output

dict_items([(‘Ramkrishna Kumar’, 0), (‘Shubham Kashyap’, 0), (‘Vishnu Singh’, 0), (‘Ayushi’, 0), (‘Satyam Keshri’, 0), (‘Mausam Mishra’, 0), (‘Jitendra Shrama’, 0), (‘Ankit Jha’, 0), (‘Birendra Yadev’, 0), (‘Yathartha Gaurav’, 0)])

get():

Returns the value associated with a given key. If the key does not exist, returns a default value.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
# Create a dictionary with default values set to 0
friends_dict = dict.fromkeys(friends, 0)
# Get the value associated with the key 'Ramkrishna Kumar'
value = friends_dict.get('Ramkrishna Kumar')
print(value)
# Trying to get the value for a non-existent key
value = friends_dict.get('Akash Raz', 'Not Found')
print(value)

Output

0

Not Found

update():

Updates the dictionary with key-value pairs from another dictionary or iterable.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
# Create a dictionary with default values set to 0
friends_dict = dict.fromkeys(friends, 0)
# Create another dictionary with some updated values
updated_values = {'Ramkrishna Kumar': 5, 'Ayushi': 3, 'Mausam Mishra' : 8, 'Shubham Kashyap' : 7, 'Jitendra Sharma' : 2}
# Update friends_dict with key-value pairs from updated_values
friends_dict.update(updated_values)
# Print the updated friends_dict
print(friends_dict)

Output

{‘Ramkrishna Kumar’: 5, ‘Shubham Kashyap’: 7, ‘Vishnu Singh’: 0, ‘Ayushi’: 3, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 8, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0, ‘Jitendra Sharma’: 2}

copy():

Returns a shallow copy of the dictionary.

Example

friends = ["Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha", "Birendra Yadev", "Yathartha Gaurav"]
# Create a dictionary with default values set to 0
friends_dict = dict.fromkeys(friends, 0)
# Create a shallow copy of the dictionary
friends_dict_copy = friends_dict.copy()
# Print the copied dictionary
print("Copied friends dictionary:",friends_dict_copy)

Output

Copied friends dictionary: {‘Ramkrishna Kumar’: 0, ‘Shubham Kashyap’: 0, ‘Vishnu Singh’: 0, ‘Ayushi’: 0, ‘Satyam Keshri’: 0, ‘Mausam Mishra’: 0, ‘Jitendra Shrama’: 0, ‘Ankit Jha’: 0, ‘Birendra Yadev’: 0, ‘Yathartha Gaurav’: 0}

  • Sets:

It is a collection of unique items.
It is used to eliminate duplicate items from a list.
Also, it supports set operations like union intersection and difference.

Example

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
# Original set of friends before any modifications
print("Original set:", friends)

Output

Original set: {‘Satyam Keshri’, ‘Ramkrishna Kumar’, ‘Shubham Kashyap’, ‘Yathartha Gaurav’, ‘Vishnu Singh’, ‘Ayushi’, ‘Jitendra Shrama’, ‘Birendra Yadev’, ‘Ankit Jha’, ‘Mausam Mishra’}

Following are the methods that you can use on sets:

  1. add()
  2. update()
  3. len()
  4. remove()
  5. pop()

Add()

Use add() method to add one item to a set

Example

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
friends.add("Birendra Yadev ")
print("After adding :", friends)

Output

After adding : {‘Ayushi’, ‘Satyam Keshri’, ‘Ankit Jha’, ‘Mausam Mishra’, ‘Vishnu Singh’, ‘Birendra Yadev ‘, ‘Jitendra Shrama’, ‘Shubham Kashyap’, ‘Ramkrishna Kumar’}

Update()

Use update() method to add more than one item

Example

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
new_friends = {"Yathartha Gaurav", "Vaishnvee "}
friends.update(new_friends)
print("After updating with :", friends)

Output

After updating with : {‘Mausam Mishra’, ‘Satyam Keshri’, ‘Ankit Jha’, ‘Vishnu Singh’, ‘Vaishnvee ‘, ‘Ayushi’, ‘Yathartha Gaurav’, ‘Shubham Kashyap’, ‘Jitendra Shrama’, ‘Ramkrishna Kumar’}

Len()

Returns the number of elements in the set

Example

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
number_of_friends = len(friends)
print("Number of friends:", number_of_friends)

Output

Number of friends: 8

Remove()

Use remove() or discard() method to remove an item in a set

Example

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
friends.remove("Ramkrishna Kumar")
print("After removing 'Ramkrishna Kumar':", friends)

Output

After removing ‘Ramkrishna Kumar’: {‘Ayushi’, ‘Ankit Jha’, ‘Shubham Kashyap’, ‘Mausam Mishra’, ‘Jitendra Shrama’, ‘Satyam Keshri’, ‘Vishnu Singh’}

Pop()

It removes a random element from the set and returns the removed element

# Original set of friends
friends = {"Ramkrishna Kumar", "Shubham Kashyap", "Vishnu Singh", "Ayushi", "Satyam Keshri", "Mausam Mishra", "Jitendra Shrama", "Ankit Jha"}
removed_friend = friends.pop()
print("Removed friend:", removed_friend)
print("After popping:", friends)

Output

Removed friend: Jitendra Shrama

After popping: {‘Satyam Keshri’, ‘Shubham Kashyap’, ‘Vishnu Singh’, ‘Ramkrishna Kumar’, ‘Ayushi’, ‘Mausam Mishra’, ‘Ankit Jha’}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top