Found 10441 Articles for Python

How to get a list of all the keys from a Python dictionary?

Nikitasha Shrivastava
Updated on 11-Jun-2025 13:10:37

50K+ Views

In this article, we will show you how to get a list of all the keys from a Python dictionary. We can get the list of all the keys from a Python dictionary using the following methods - Using dict.keys() Method Using list() & dict.keys() Function Using List Comprehension ... Read More

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

Nikitasha Shrivastava
Updated on 11-Jun-2025 12:47:53

34K+ Views

In this article, we will show you how to get a value for the given key from a dictionary in Python. Below are the 4 different methods to accomplish this task - Using Dictionary Indexing Using dict.get() Method Using keys() Function Using items() Function Assume we have taken a dictionary containing key-value pairs. We will return the value for a given key from a given input dictionary. Using dictionary indexing In Python, we can retrieve the value from a dictionary by using dict[key]. Example 1 The following program returns the index of the value for a given key ... Read More

How to convert a list into a tuple in Python?

Nikitasha Shrivastava
Updated on 16-Jun-2025 15:01:33

26K+ Views

List and Tuple in Python are classes of data structures. The list is dynamic, whereas the tuple has static characteristics. In this article, we will convert a list into a tuple by using a few methods. Each of these methods is discussed below. Converting List into Tuple using tuple() We can convert a list into a tuple using the tuple() function. This function accepts a list as a parameter, converts into a tuple and returns the result.Example 1 In the following program, we convert a list to a tuple using the tuple() function. # using tuple() built-in function list_names=['Meredith', 'Kristen', 'Wright', ... Read More

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

Nikitasha Shrivastava
Updated on 11-Jun-2025 10:52:32

6K+ Views

A tuple is an ordered and immutable collection of Python objects separated by commas. Like lists, tuples are sequences. Tuples differ from lists in that they can not be modified, whereas lists can, and they use parentheses instead of square brackets. Though we can not change individual elements of a tuple, the del operator works with them. In this article, we are going to discuss the use of the del operator on a tuple. The del Operator with Tuples The del operator in Python is used to delete variables from the current scope. When you use "del" on a tuple ... Read More

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

Nikitasha Shrivastava
Updated on 11-Jun-2025 10:45:32

6K+ Views

In Python, the "*" operator can be used to repeat a tuple multiple times. This is known as tuple repetition. It creates a new tuple with repeated elements but does not modify the original tuple. Following are some points to remember - The repetition operator (*) creates a new tuple. Individual items are not repeated, only the entire tuple. The repeat count must be a non-negative integer. The original tuple is unaltered. Accepts tuples of any length and mixed data types. How Repetition Operator Works? When you apply '*' to a tuple with an integer, Python duplicates the ... Read More

How does concatenation operator work on tuple in Python?

Nikitasha Shrivastava
Updated on 11-Jun-2025 10:37:14

1K+ Views

The concatenation operator (+) is basically used to join two or more tuples together in Python. This operation returns a new tuple that contains all the items of the original tuples. In this article, we will discuss how the concatenation operator works on tuples in Python. But first, let us see how concatenation works. How Concatenation Works? When you use the concatenation operator on tuples, Python creates a new tuple that includes all the elements from the tuples being concatenated. The original tuples remain unchanged. As you may know, tuples in Python are immutable means their values cannot be changed ... Read More

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

Nikitasha Shrivastava
Updated on 11-Jun-2025 10:30:36

3K+ Views

Python offers the 'in' operator to verify that a value is present in a tuple. This operator is very useful when you are looking for items in a collection without requiring loops or complex logic. In this article, we will discuss the 'in' operator and how it works on tuples in Python. Before moving on, we will first discuss tuples. Tuples in Python are an immutable sequence, and they are created by placing a sequence of values separated by a 'comma', with or without the use of parentheses for data grouping. Tuples can have any number of elements and any ... Read More

Why do you think tuple is an immutable in Python?

Nikitasha Shrivastava
Updated on 11-Jun-2025 10:19:17

5K+ Views

Tuples in Python are immutable, which means once we create them, we can not change their items. In this article, we will discuss why tuples are immutable before moving on, and we will understand tuples in detail. Tuples in Python Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they have the property of being immutable. We can't change the elements of a tuple, but we can perform operations suchas counting the number of elements, accessing elements using an index, retrieving the type of the elements, etc. Tuples ... Read More

What is the difference between a python tuple and a dictionary?

Nikitasha Shrivastava
Updated on 26-May-2025 16:58:46

18K+ Views

Python offers many built-in data structures like lists, tuples, sets, and dictionaries, which are used to store and manage data easily. In this article, we will discuss the difference between a Python tuple and a dictionary. Tuple Tuples are a data type that belongs to the sequence data type category. They are similar to lists in Python, but they are immutable. We can't change the elements of a tuple, but we can execute a variety of actions on them, such as count, index, type, etc. Tuples are created in Python by placing a sequence of values separated by a 'comma', with or ... Read More

What is the difference between a python list and an array?

Nikitasha Shrivastava
Updated on 16-May-2025 18:40:55

3K+ Views

In this article we are going to discuss about the difference between Python list and array. As you may know both of these are ways to store a collection of items like a bunch of numbers or words but they are not exactly the same. So understanding how they are different will help you select the right one when you are writing your program. List Lists are one of the four most commonly used data structures provided by Python. A list is a data structure in python that is mutable and has an ordered sequence of elements. Lists also support ... Read More

Advertisements