Program to convert a list of tuples in a dictionary

In this python tuple program, we will convert a list of tuples in a dictionary.

Steps to solve the program
  1. Take a list of tuples as input and create an empty dictionary.
  2. Use for loop to iterate over each tuple in the list.
  3. Use D.setdefault(a, []).append(b) it will set the first value in the tuple as the key and create an empty list to store its values.
  4. a is the key and b is the valueis the name of the dictionary.
  5. If there is a tuple in the list whose key has been previously used then it will not create a new key-value pair instead it will add the value of the tuple to the existing list of key-value pair.
  6. Print the output.
				
					l =  [('s',2),('q',1),('a',1),('s',3),('q',2),('a',4)]
print("List of tuples: ",l)
D = {}
for a, b in l:
    D.setdefault(a, []).append(b)

print("Dictionary: ",D)
				
			

Output :

				
					List of tuples:  [('s', 2), ('q', 1), ('a', 1), ('s', 3), ('q', 2), ('a', 4)]
Dictionary:  {'s': [2, 3], 'q': [1, 2], 'a': [1, 4]}
				
			

program to reverse a tuple.

Python tuple program to reverse a tuple

In this python tuple program, we will reverse a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. FIrst reverse the given tuple by using reversed() and then convert it to a tuple using tuple().
  3. Print the output.
				
					tup = (4,6,8,3,1)
print("Original tuple: ",tup)
tup1 = tuple(reversed(tup))
print("Reversed tuple: ",tup1)
				
			

Output :

				
					Original tuple:  (4, 6, 8, 3, 1)
Reversed tuple:  (1, 3, 8, 6, 4)
				
			

convert a tuple into a dictionary.

convert a list of tuples in a dictionary.

Python tuple program to convert a tuple into a dictionary

In this python tuple program, we will convert a tuple into a dictionary.

Steps to solve the program
  1. Take tuples inside a tuple as input.
  2. Convert the tuple into a dictionary using dict().
  3. Print the output.
				
					tup = ((5,'s'),(6,'l'))
print("Tuple: ",tup)
D = dict(tup)
print("Dictionary: ",D)
				
			

Output :

				
					Tuple:  ((5, 's'), (6, 'l'))
Dictionary:  {5: 's', 6: 'l'}
				
			

find the length of a tuple.

program to reverse a tuple.

Python tuple program to find the length of a tuple

In this python tuple program, we will find the length of a tuple.

Steps to solve the program
  1. Take a tuple as input.
  2. Find the length of a tuple using len().
  3. Print the output.
				
					tup = ('v','i','r','a','t')
print("Original tuple: ",tup)
print("Length of the tuple: ",len(tup))
				
			

Output :

				
					Original tuple:  ('v', 'i', 'r', 'a', 't')
Length of the tuple:  5
				
			

find an index of an element in a tuple.

convert a tuple into a dictionary.

Python tuple program to slice a tuple

In this python tuple program, we will slice a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Slice the given tuple using tuple_name[start_index:end_index].
  3. Print the output.
				
					tup = (5,7,3,4,9,0,2)
print("Original tuple: ",tup)
print(tup[:3])
print(tup[2:5])
				
			

Output :

				
					Original tuple:  (5, 7, 3, 4, 9, 0, 2)
(5, 7, 3)
(3, 4, 9)
				
			

remove an item from a tuple.

find an index of an element in a tuple.

Python tuple program to remove an item from a tuple

In this python tuple program, we will remove an item from a tuple.

Steps to solve the program
  1. Create a tuple.
  2. Now convert that tuple to a list using list().
  3. Remove the element from the list.
  4. Again convert the list to a tuple using tuple().
  5. Print the output.
				
					tup = ('p','y','t','h','o','n')
print("Original tuple: ",tup)
l = list(tup)
l.remove('h')
tup = tuple(l)
print("After removing an element: ",tup)
				
			

Output :

				
					Original tuple:  ('p', 'y', 't', 'h', 'o', 'n')
After removing an element:  ('p', 'y', 't', 'o', 'n')
				
			

convert a list into a tuple and multiply each element by 2.

program to slice a tuple.

Convert a list into a tuple and multiply each element

In this pythin tuple program, we will convert a list into a tuple and multiply each element.

Steps to solve the program
  1. Create a list and an empty list.
  2. Use for loop to iterate over elements in the list.
  3. Multiply each element by 2 and add it to the empty list.
  4. Now convert the new list to a tuple using tuple().
  5. Print the output.
				
					list1 = [12,65,34,77]
list2 = []
for ele in list1:
    a = 2*ele
    list2.append(a)
    
tup = tuple(list2)
print("Origianl list: ",list1)
print("After multiplyting by 2: ",tup)
				
			

Output :

				
					Origianl list:  [12, 65, 34, 77]
After multiplyting by 2:  (24, 130, 68, 154)
				
			

remove an item from a tuple.

Python tuple program to multiply adjacent elements of a tuple

In this python tuple program, we will multiply adjacent elements of a tuple.

Steps to solve the program
  1. Create a tuple and an empty list.
  2. Create a tuple having adjacent elements as a tuple inside of a tuple using zip(tup,tup[1:]).
  3. Use for loop to iterate over this tuple.
  4. Now get the product of the adjacent element and add it to the empty list.
  5. Convert the list back to the tuple using tuple().
  6. Print the output.
				
					tup =  (1,2,3,4)
list1 = []
for a,b in zip(tup,tup[1:]):
    c = a*b
    list1.append(c)
tup = tuple(list1)
print("Multiplying adjacent elements: ",tup)
				
			

Output :

				
					Multiplying adjacent elements:  (2, 6, 12)
				
			

create a tuple having squares of the elements from the list.

Python Projects For Beginners

This article has common Python Projects for beginners, which have all categories of projects from basic to advanced.

Basic Level Projects:

  • Basic Calculator
  • Show Calendar
  • Convert pdf to text
  • Convert image to pdf
  • Convert dictionary to JSON
  • Convert JPEG to PNG
  • Convert JSON to CSV
  • Convert XML to JSON
  • Convert book into audiobook 
  • Decimal to binary convertor and vice versa
  • DNS Records
  • Encrypt and decrypt text
  • Extract zip files
  • Fetch HTTP status code
  • Fetch open ports
  • Hashing passwords
  • Merge csv
  • Merge pdf
  • QR code generator
  • Random password generator

Intermediate Level Projects:

  • Capture screenshot
  • Calculate age
  • Chrome Automation
  • Convert numbers into words
  • Script to encrypt files and folders
  • Article Downloader
  • Duplicate file remover
  • Email GUI
  • Email verification
  • Facebook dp downloader
  • Get wifi password
  • Github repo automation
  • Image to speech
  • Language Translator
  • Password manager
  • Plagiarism checker
  • Python image compressor
  • Send an email with a python
  • Send mail from CSV
  • Set alarm
  • URL shortener

Advance Level Projects:

  • Auto birthday wisher.
  • Auto Backup.
  • Auto-fill the google form.
  • Automate Facebook bot.
  • Automatic certificate generator.
  • Automatic Facebook login
  • Calculator GUI
  • Calendar GUI
  • CLI todo
  • Simple Stopwatch
  • Dictionary GUI
  • Easy cartoonify the image
  • Video Player
  • Extract text from PDF
  • Gesture control media player
  • Google meeting bot
  • Image to sketch
  • Noise reduction script
  • Password manager GUI
  • PDF reader with voice
  • Quote scraper
  • Screen recorder
  • Speech to text
  • Text to speech
  • Text editor
  • Voice Translator
  • Video to audio converter