Found 27104 Articles for Server Side Programming

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

100K+ 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

543 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

749 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

What's the difference between lists and tuples in Python?

Malhar Lathkar
Updated on 30-Jul-2019 22:30:21

458 Views

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. However, main difference between list and tuple is that list object is mutable whereas tuple object is immutable. Immutable object can not be modified once it is created in memory. Hence it is not possible to add, modify or remove item from tuple object. On the other hand these operations can be performed on a list.

How to get the second-to-last element of a list in Python?

Malhar Lathkar
Updated on 20-Feb-2020 11:04:00

22K+ Views

Python sequence, including list object allows indexing. Any element in list can be accessed using zero based index. If index is a negative number, count of index starts from end.  As we want second to last element in list, use -2 as index.>>> L1=[1,2,3,4,5] >>> print (L1[-2]) 4

How to get the last element of a list in Python?

Vikram Chiluka
Updated on 23-Aug-2023 12:55:56

80K+ Views

In this article, we will show you how to get the last element of the input list using python. Now we see 4 methods to accomplish this task − Using negative indexing Using slicing Using pop() method Using For loop Assume we have taken a list containing some elements. We will return the last element of the given input list using different methods as specified above. Method 1: Using negative indexing Python allows for "indexing from the end, " i.e., negative indexing. This means that the last value in a sequence has an index of −1, the second ... Read More

How to append list to second list (concatenate lists) in Python?

Vikram Chiluka
Updated on 19-Sep-2022 09:30:17

2K+ Views

In Python, a list is an ordered sequence that can hold several object types such as integer, character, or float. In other programming languages, a list is equivalent to an array. In this article, we are going to append the list to another list(Concatenate Lists) in Python. The following are the different methods to accomplish this task − Using Concatenation(+) Operator Using the append method of the list object Using extend() Method Using itertools.chain() Method Obtain a list without duplicates Assume we have taken a list containing some elements. We will return the concatenated list of the given ... Read More

What is difference in getattr() and setattr() functions in Python?

Rajendra Dharmkar
Updated on 20-Feb-2020 10:39:57

330 Views

The getattr() methodThe getattr() method returns the value of the named attribute of an object. If not found, it returns the default value provided to the function.SyntaxThe syntax of getattr() method is −getattr(object, name[, default])The getattr() method can take multiple parameters −The getattr() method returns −value of the named attribute of the given objectdefault, if no named attribute is foundAttributeError exception, if named attribute is not found and default is not definedThe setattr() methodThe setattr() method sets the value of given attribute of an object.SyntaxThe syntax of setattr() method is −setattr(object, name, value)The setattr() method takes three parameters −The setattr() ... Read More

Advertisements