Found 34486 Articles for Programming

How can I do Python Tuple Slicing?

Malhar Lathkar
Updated on 20-Feb-2020 11:27:57

299 Views

Slicing operator can be used with any sequence data type, including Tuple. Slicing means separating a part of a sequence, here a tuple. The symbol used for slicing is ‘:’. The operator requires two operands. First operand is the index of starting element of slice, and second is index of last element in slice+1. Resultant slice is also a tuple.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[2:4] (20, 9)Both operands are optional. If first operand is missing, slice starts from beginning. If second operand is missing, slice goes upto end.>>> T1=(10,50,20,9,40,25,60,30,1,56) >>> T1[6:] (60, 30, 1, 56) >>> T1[:4] (10, 50, 20, 9)

How to pop-up the first element from a Python tuple?

Pythonic
Updated on 20-Feb-2020 11:27:15

3K+ Views

By definition, tuple object is immutable. Hence it is not possible to remove element from it. However, a workaround would be convert tuple to a list, remove desired element from list and convert it back to a tuple.>>> T1=(1,2,3,4) >>> L1=list(T1) >>> L1.pop(0) 1 >>> L1 [2, 3, 4] >>> T1=tuple(L1) >>> T1 (2, 3, 4)

What is the difference between __str__ and __repr__ in Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

138 Views

The built-in functions repr() and str() respectively call object.__repr__(self) and object.__str__(self) methods. First function computes official representation of the object, while second returns informal representation of the object. Result of both functions is same for integer object. >>> x = 1 >>> repr(x) '1' >>> str(x) '1' However, it is not the case for string object. >>> x = "Hello" >>> repr(x) "'Hello'" >>> str(x) 'Hello' Return value of repr() of a string object can be evaluated by eval() function and results in valid string object. However, result of str() can not be evaluated. ... Read More

How do I check whether a file exists using Python?

Pythonic
Updated on 30-Jul-2019 22:30:21

521 Views

Presence of a certain file in the computer can be verified by two ways using Python code. One way is using isfile() function of os.path module. The function returns true if file at specified path exists, otherwise it returns false. >>> import os >>> os.path.isfile("d:\Package1\package1\fibo.py") True >>> os.path.isfile("d:/Package1/package1/fibo.py") True >>> os.path.isfile("d:onexisting.txt") Note that to use backslash in path, two backslashes have to be used to escape out of Python string. Other way is to catch IOError exception that is raised when open() function has string argument corresponding to non-existing file. try: fo = open("d:onexisting.txt", ... Read More

How to remove index list from another list in python?

Vikram Chiluka
Updated on 19-Sep-2022 11:03:49

2K+ Views

In this article, we will show you how to remove the index list elements from the original list using python. Now we see 2 methods to accomplish this task − Using pop() method Using del keyword Assume we have taken a list containing some elements. We will remove the index list elements from the main list using different methods as specified above. Note We must sort the indices list in descending order because removing elements from the beginning will change the indices of other elements, and removing another element will result in an incorrect result due to misplaced ... Read More

How to join list of lists in python?

Vikram Chiluka
Updated on 19-Sep-2022 10:38:44

24K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In this article, we will show you how to join the list of lists(nested lists) using python. Now we see 4 methods to accomplish this task − Using nested for loop Using List comprehension Using sum() function Using NumPy module Assume we have taken a list of lists containing some elements. We will join those list of lists and return the result using different methods as specified above. Method 1: Using nested for loop Algorithm (Steps) Create a ... Read More

How to remove an element from a list by index in Python?

Vikram Chiluka
Updated on 23-Aug-2023 13:01:13

101K+ Views

In this article, we will show you the remove an element from a list by index using Python. Here we see 4 methods to accomplish this task − Using the del keyword to remove an element from the list Using the pop() function to remove an element from the list Using slicing to remove an element from the list Using indices to remove multiple elements from the list Assume we have taken a list containing some elements. We will remove a specific item from a list by giving the index value using the above-specified methods. Method 1: Using ... Read More

How to create a tuple from a string and a list of strings in Python?

Malhar Lathkar
Updated on 20-Feb-2020 11:22:49

122 Views

The built-in function tuple() converts a Python string into tuple of individual characters. It also turns a list object into a tuple.>>> tuple("TutorialsPoint") ('T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's', 'P', 'o', 'i', 'n', 't') >>> L1=[45, 32, 100, 10, 24, 56] >>> tuple(L1) (45, 32, 100, 10, 24, 56)

How to find the index of an item given a list containing it in Python?

Malhar Lathkar
Updated on 20-Feb-2020 11:22:14

559 Views

Position of an element in a list (any sequence data type for that matter) is obtained by index() method. This method finds first instance of occurrence of given element.>>> L1=[45, 32, 100, 10, 24, 56] >>> L1.index(24) 4

What are the differences and similarities between tuples and lists in Python?

Malhar Lathkar
Updated on 20-Feb-2020 11:21:13

756 Views

Both List and Tuple are called as sequence data types of Python. Objects of both types are comma separated collection of items not necessarily of same type.SimilaritiesConcatenation, repetition, indexing and slicing can be done on objects of both types>>> #list operations >>> L1=[1, 2, 3] >>> L2=[4, 5, 6] >>> #concatenation >>> L3=L1+L2 >>> L3 [1, 2, 3, 4, 5, 6] >>> #repetition >>> L1*3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> #indexing >>> L3[4] 5 >>> #slicing >>> L3[2:4] [3, 4]>>> #tuple operations >>> T1=(1, 2, 3) >>> T2=(4, 5, 6) >>> #concatenation >>> T3=T1+T2 >>> ... Read More

Advertisements