How to get a value for a given key from a Python dictionary?

Rajendra Dharmkar
Published on 05-Jan-2018 13:08:03
You can get the value for a given key from a Python dictionary using the [] operator on the dictionary and passing the key as an argument.examplemy_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'} print(my_dict['name']) print(my_dict['time'])OutputThis will give the output −TutorialsPoint 15 yearsYou can also use the get method ... Read More

How to check if a key exists in a Python dictionary?

Rajendra Dharmkar
Published on 05-Jan-2018 13:05:49
You can check if a key exists in a Python dictionary using the in operator. The in operator matches the key against dictionary and checks for the presence of key. examplemy_dict = {'name': 'TutorialsPoint', 'time': '15 years', 'location': 'India'} print('name' in my_dict) print('foo' in my_dict)OutputThis will give the output −True FalseRead More

How to remove all the elements of a dictionary in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 13:04:22
To remove all elements from a dictionary, the easiest way is to reassign the dictionary to an empty dictionary. examplemy_dict = {'name': 'foo', 'age': 28} my_dict = {} print(my_dict)OutputThis will give the output −{}You can also use the del function to delete a specific key or loop through all keys and ... Read More

How to convert a list into a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 13:02:29
You can convert a list into a tuple simply by passing to the tuple function. Examplemy_list = [1, 2, 3] my_tuple = tuple(my_list) print(my_tuple)OutputThis will give the output −(1, 2, 3)

How to index and slice a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 13:00:43
To index or slice a tuple you need to use the [] operator on the tuple. When indexing a tuple, if you provide a positive integer, it fetches that index from the tuple counting from the left. In case of a negative index, it fetches that index from the tuple ... Read More

How does the del operator work on a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 12:57:46
Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded using list comprehensions or slicing. To explicitly remove an entire tuple, you can use the del statement. exampletup = ('physics', 'chemistry', 1997, 2000) print(tup) del(tup) print("After deleting tup ... Read More

How does the repetition operator work on a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 12:55:25
We are accustomed to using the * symbol to represent multiplication, but when the operand on the left side of the * is a tuple, it becomes the repetition operator. The repetition operator makes multiple copies of a tuple and joins them all together. Tuples can be created using the ... Read More

How does concatenation operator work on tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 12:52:33
The concatenation operator creates a new tuple in Python using the initial tuples in the order they were added in. This is not an inplace operation. exampletuple1 = [1, 2, 3] tuple2 = ['a', 'b'] tuple3 = tuple1 + tuple2 print(tuple3)OutputThis will give the output −[1, 2, 3, 'a', 'b']

How does the 'in' operator work on a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 12:48:42
Python's in operator lets you loop through all the members of a collection(such as a list or a tuple) and check if there's a member in the tuple that's equal to the given item.  examplemy_tuple = (5, 1, 8, 3, 7) print(8 in my_tuple) print(0 in my_tuple)OutputThis will give the output ... Read More

How does the * operator work on a tuple in Python?

Rajendra Dharmkar
Published on 05-Jan-2018 12:45:30
The star(*) operator unpacks the sequence/collection into positional arguments. So if you have a tuple and want to pass the items of that tuple as arguments for each position as they are there in the tuple, instead of indexing each element individually, you could just use the * operator. exampledef multiply(a, ... Read More
Advertisements