Python Simple program iteration through list - python

I'm new to programming and I'm making a simple program to test.
Here is the code:
list1 = [1,2,d,,t,h,7,8]
for x in list1:
if x ==
I'm trying to iterate in my list and check to see which item is a string and which is
a number(I know its basic, but im new). So what would be a correct way to write this line
of code. All suggestions would help

In Python, use the builtin isinstance(variable, type) to test whether a variable is of a given type.
The type variable can also be a tuple of several types to test against.

Your list is a little messed up. If those letters are supposed to be strings, it should look like this:
list1 = [1,2,'d','t','h',7,8]
Otherwise they are referring to variables.
To check if number
for x in list1:
if isinstance(x, int):
...
To check if string
for x in list1:
if isinstance(x, str):
...
Combination
for x in list1:
if x.isinstance(x, int):
....
elif isinstance(x, str)L
....

This should print the type of each element in your list
for item in list1:
print type(item)

Assuming your list contains purely numbers and strings:
for x in list1:
if type(x) == str:
# do something
else:
# do something for numbers

Since everyone is giving answers similar to my old one (see below), I'll try something else.You can easily make 2 lists that separate ints and strings with Python's list comprehensions:
strings = [x for x in list1 if isinstance(x, str)]
intlist = [x for x in list1 if isinstance(x, int)]
Using list comprehensions made the code more compact and easier to understand.
Old response:
list1 = [1, 4, 5, '5', '3', 3, 'a']
for x in list1:
if(isinstance(x, str)):
print(str(x) + ' is a string.')
else:
print(str(x) + ' is not a string.')

You could try this one,
list1=[1,2,'d','t','h',7,8]
for data in list1:
if type(data) is str:
#do something with it
elif type(data) is int:
#do something with it
you can try something like this or any of those methods mentioned from other answers in the python interpreter to see it works -
>> type(1) is str
and you would get
>> False

Related

Converting integer of tuple to string of tuple

I wanted to convert the integer of tuple into string of tuple.
For example:
data = [(2,3,4,...),(23,42,54,...),......]
would result in:
d = [('2','3','4',...),('23','42','54',....)......]
Please notice how tuple is big and list is also big.
I tried the code below but it doesn't give me the result I was looking for:
data = [(2,3,4,...),(23,42,54,...),......]
d=[]
for t in data:
d.append(str(t))
This gets close:
data = [[str(x) for x in tup] for tup in data]
If you actually need tuples:
data = [tuple(str(x) for x in tup) for tup in data]
Or, if you prefer a more "functional" approach:
data = [tuple(map(str, tup)) for tup in data]
Though it seems that map has fallen out of favor with the python crowd (or maybe it never was actually in favor ;-).
This will recursively convert infinitely nested lists and tuples:
def stringify(something):
if type(something) == list:
return [stringify(x) for x in something]
elif type(something) == tuple:
return tuple(stringify(list(something)))
else:
return str(something)

Printing a list with string.format rounding for float members

If I have a list of the format ['ABC',0.1234,'DEF'] and I wanted to print it out doing something like
l = ['ABC',0.1234,'DEF']
print('\t'.join(map(str, l)))
But I would also like to round the float so that the output would be ABC\t0.12\tDEF.
I can do it with something like:
a,b,c = l
print('{}\t{:.2f}\t{}'.format(a,b,c))
But since l is just one member of a much larger list, I would like to be able to do this with a list comprehension or something more pythonic.
As mentioned in the comment, you can simply do this for a big list:
print('{}\t{:.2f}\t{}'.format(*l))
Note that .format() also supports indexing the list. Something like:
print('0[0] 0[1] 0[2]'.format(l))
Edit:
"The data structure here is a list of triplet lists. Is there any straightforward way to do string formatting on a unknown size list?"
You can try:
>>> print('\t'.join("{:.2f}".format(x) if isinstance(x, float) else str(x) for k
in l for x in k))
You could use:
print('\t'.join("{:.2f}".format(x) if isinstance(x, float) else str(x) for x in l))
You could define a function for this:
def float_or_str(x):
try:
return '{:.2f}'.format(x)
except ValueError:
return '{}'.format(x)
l = ['ABC',0.1234,'DEF']
print('\t'.join(map(float_or_str, l)))
yields
ABC 0.12 DEF

Python logic error?

Basically, I'm trying to flatten a list in my function but ignore that (you can also ignore the print functions I put in).
take x = [[1,2,3],4,5] to be my variable.
I call prob7(x) but the issue is that when type([1,2,3]) gets checked == list, it returns false. Why is that? I explicitly check this on the interpreter command line and it returns true. But inside the function, I get a false.
Just a bug that I missed because I'm sleepy or am I misunderstanding some part of the Python language? I run version 2.6 if it matters.
def prob7(list): # flatten a list
tempList = []
if list: # meaning if there are elements in the list and it is not empty
for i in list:
if type(i) != list:
print tempList,'if',i,type(i)==list
tempList.append(i)
else:
print tempList,'else',i
tempList.extend(prob7(i))
return tempList
Just not use 'list' as a variable name and use isinstance(var, list) instead of type(var) == list.
Please find corrected sample below.
def prob7(mylist): # flatten a list
tempList = []
if mylist: # meaning if there are elements in the list and it is not empty
for i in mylist:
if not isinstance(i, list):
print tempList, 'if', i, isinstance(i, list)
tempList.append(i)
else:
print tempList, 'else', i
tempList.extend(prob7(i))
return tempList
Or if you don't really required to use recursion and you don't care about values order then you can use something like this:
lVals = [[1,2,3],4,5, [1,[4,7]]]
def make_flat(mylist): # flatten a list
while any(isinstance(x, list) for x in mylist):
for i, val in enumerate(mylist):
if isinstance(val, list):
mylist.extend(mylist.pop(i))
break
return mylist
make_flat(lVals)
>>> [4, 5, 1, 2, 3, 1, 4, 7]
Artisom has your answer. In addtion, type checks are not very Pythonic. Duck typing often is the way to go. In case your elements are numbers only, the following does the job too, without explicit type checks but behavior checks:
def prob7(inlist): # flatten a list
outlist = []
for x in inlist:
try:
outlist += x
except TypeError:
outlist.append(x)
return outlist
Note that string elements in this implementation would behave like nested lists. Anyway, just wanted to illustrate what it means to expect behavior, not types.
Some alternate approaches:
# Iterative, but more functional-style
def flatten(a_list):
while any(isinstance(x, list) for x in a_list):
a_list = sum((x if isinstance(x, list) else [x] for x in a_list), [])
return a_list
# Using a generator recursively,
# then evaluating the generator to produce the list
# instead of explicitly appending each element.
def flatten_gen(a_list):
for x in a_list:
if isinstance(x, list):
for y in flatten_gen(x): yield y
else: yield x
def flatten(a_list): return list(flatten_gen(a_list))
The problem here is you are using a local variable name (list) that is the same as the global list type. You should change your variable name. Also, when checking types like that you can use the is operator.
type(l) is list
But here's my version of flatten.
def flatten(alist):
rv = []
for val in alist:
if isinstance(val, list):
rv.extend(flatten(val))
else:
rv.append(val)
return rv
This does not alter the original list, but returns a new list. This is consistent with most other patterns.

How to test if every item in a list of type 'int'?

Say I have a list of numbers. How would I do to check that every item in the list is an int?
I have searched around, but haven't been able to find anything on this.
for i in myList:
result=isinstance(i, int)
if result == False:
break
would work, but looks very ugly and unpythonic in my opinion.
Is there any better(and more pythonic) way of doing this?
There are a few different ways to do it. For example, if your list includes only numbers:
>>> my_list = [1, 2, 3.25]
>>> all(isinstance(item, int) for item in my_list)
False
>>> other_list = range(3)
>>> all(isinstance(item, int) for item in other_list)
True
>>>
Anyways, this solution doesn't work as expected if your list includes booleans, as remarked by #merlin:
>>> another_list = [1, 2,False]
>>> all(isinstance(item, int) for item in another_list)
True
If your list include booleans you should use type instead of isinstance (it' a little slower, but works as you expect):
>>> another_list = [1, 2, False]
>>> all(type(item) is int for item in another_list)
False
>>> last_list = [1, 2, 3]
>>> all(type(item) is int for item in last_list)
True
The following statement should work. It uses the any builtin and a generator expression:
any(not isinstance(x, int) for x in l)
This will return true if there is a non-int in the list. E.g.:
>>> any(not isinstance(x, int) for x in [0,12.])
True
>>> any(not isinstance(x, int) for x in [0,12])
False
The all builtin could also accomplish the same task, and some might argue it is makes slightly more sense (see Dragan's answer)
all(isinstance(x,int) for x in l)
One approach would not be to test, but to insist. This means your program can handle a broader range of inputs intelligently -- it won't fail if someone passes it a float instead.
int_list = [int(x) for x in int_list]
or (in-place):
for i, n in enumerate(int_list):
int_list[i] = int(n)
If something can't be converted, it will throw an exception, which you can then catch if you care to.
In [1]: a = [1,2,3]
In [2]: all(type(item)==int for item in a)
Out[2]: True
See functions
def is_int(x):
if type(x) == int:
return True
return
def all_int(a):
for i in a:
if not is_int(i):
return False
return True
Then call
all_int(my_list) # returns boolean
Found myself with the same question but under a different situation: If the "integers" in your list are represented as strings (e.g., as was the case for me after using 'line.split()' on a line of integers and strings while reading in a text file), and you simply want to check if the elements of the list can be represented as integers, you can use:
all(i.isdigit() for i in myList)
For example:
>>> myList=['1', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList)
True
>>> myList2=['1.5', '2', '3', '150', '500', '6']
>>> all(i.isdigit() for i in myList2)
False
lst = [1,2,3]
lst2 = [1,2,'3']
list_is_int = lambda lst: [item for item in lst if isinstance(item, int)] == lst
print list_is_int(lst)
print list_is_int(lst2)
suxmac2:~$ python2.6 xx.py
True
False
....one possible solution out of many using a list comprehension or filter()

python extend or append a list when appropriate

Is there a simple way to append a list if X is a string, but extend it if X is a list? I know I can simply test if an object is a string or list, but I was wondering if there is a quicker way than this?
mylist.extend( [x] if type(x) == str else x )
or maybe the opposite would be safer if you want to catch things other than strings too:
mylist.extend( x if type(x) == list else [x] )
I do not think so. extend takes any iterable as input, and strings as well as lists are iterables in python.
buffer = ["str", [1, 2, 3], 4]
myList = []
for x in buffer:
if isinstance(x, str):
myList.append(x)
elif isinstance(x, list):
myList.extend(x)
else:
print("{} is neither string nor list".format(x))
A better way would be using try-except instead of isinstance()

Categories

Resources