Using * to create ntuples (unexpected behavior) [duplicate] - python

This question already has answers here:
What is the syntax rule for having trailing commas in tuple definitions?
(10 answers)
Closed 5 years ago.
Can somebody explain why this creates a list of 5 elements:
['']*5
While this create a tuple of 1 element
('')*5
But this creates a tuple with six elements:
('', '')*3
Question: is there a way to create tuple with an odd number of elements without using a generator (i.e. by using *)?

('') is actually not a tuple, it is a string. You want to write ('',).

Related

Split list element into lists in python [duplicate]

This question already has answers here:
Apply function to each element of a list
(4 answers)
Python Splitting Array of Strings
(6 answers)
Split each string in list and store in multiple arrays
(4 answers)
Closed 1 year ago.
I have a list
list = ['a:b:c:d', 'e:f:g:h', 'i:j:k:l']
What I'm trying to do is make a list of each of it's elements. Something like this:
listelement[0] = list[0].split(':')
print(listelement[0][1])
output: b
I can't seem to figure out how to create something that works similarly to this.
You can try list comprehension to do what you want.
new_list = [element.split(':') for element in list].
Also I would advise against the use of list as a name, since it's a reserved word.

how to determine if a tuple doesnt has an empty input Python [duplicate]

This question already has answers here:
Remove Tuple if it Contains any Empty String Elements
(2 answers)
What is the best way to check if a tuple has any empty/None values in Python?
(5 answers)
Remove a tuple containing nan in list of tuples -- Python
(4 answers)
Closed 4 years ago.
I'm simply trying to determine if my tuple has empty inputs. I'm reading Data from an Excel Sheet and it gives tuples in the form
(3,4,6), (3,4,7)
so basically it typically has 3 parameters but there are some cells that don't include the first or second parameter, so technically it would be
('','',6)
In my code I have set up as:
for i in range(len(self.listOfTuples)):
for j in range(len(self.listOfTuples[i])):
if(self.listOfTuples[i][j][0] == '' or self.listOfTuples[i][j][1] == ''):
print("GOT IT ")
and it's not printing my statement, when I print out the self.listofTuples[i][j][0]. It actually prints out nan, but when I change my if statement with "nan" it still doesn't print out my statement. Is there something i'm missing ?
nan is "not-a-number". You can detect it with math.isnan(self.listOfTuples[i][j][0]).

How to remove last string in list in Python 3.5 [duplicate]

This question already has answers here:
How to delete the very last character from every string in a list of strings
(5 answers)
Closed 5 years ago.
Suppose,
a = ["Power is nothing"]
How to remove last string in variable 'a' which is 'g'?
You can use list slicing:
a = a[0][:-1]
a[0] selects the first element in your list. in your case, a[0] is "Power is nothing". Then [:-1] returns a new string without the last letter.
You can check the documentation for any detail regarding list slicing in python

Python iterate through a list of tuples [duplicate]

This question already has answers here:
Find an element in a list of tuples
(10 answers)
How to check if all elements of a list match a condition?
(5 answers)
Closed 5 years ago.
I am new in Python and I was curious if I can do something like this:
Let's say that I have a list of tuples and one variable like this:
list = [(123,"a"),(125,"b")]
variable = (123,"c")
Is it possible to search for the first element of the variable in the list like this?
if variable[0] in list[0]:

how to define a list with predefined length in Python [duplicate]

This question already has answers here:
Create a list with initial capacity in Python
(11 answers)
Closed 9 years ago.
I want to do the following :
l = list()
l[2] = 'two'
As expected, that does not work. It returns an out of range exception.
Is there any way to, let say, define a list with a length ?
Try this one
values = [None]*1000
In place of 1000 use your desired number.

Categories