I have searched and found nothing that suits my needs.
I have three for loops that each has an if conditions inside it. Here is the code:
for friend in friends:
columns = friend.split("\n")
if len(columns) == 4:
c.execute("""INSERT INTO `fb_friends`(`name`, `no_of_mutual_friends`, `no_of_new_posts`, `already_friends`) VALUES (?, ?, ?, ?)""", (columns))
for friend in friends:
columns = friend.split("\n")
if len(columns) == 3:
c.execute("""INSERT INTO `fb_friends`(`name`, `no_of_mutual_friends`, `already_friends`) VALUES (?, ?, ?)""", (columns))
for friend in friends:
columns = friend.split("\n")
if len(columns) == 2:
c.execute("""INSERT INTO `fb_friends`(`name`, `no_of_mutual_friends`, `already_friends`) VALUES (?, ?, ?)""", (columns))
Is there a more efficient way of doing this? I was thinking of the and operator like so:
for friend in friends:
columns = friend.split("\n")
if len(columns) == 4:
c.execute("""INSERT INTO `fb_friends`(`name`, `no_of_mutual_friends`, `no_of_new_posts`, `already_friends`) VALUES (?, ?, ?, ?)""", (columns))
and
if len(columns) == 3:
c.execute("""INSERT INTO `fb_friends`(`name`, `no_of_mutual_friends`, `already_friends`) VALUES (?, ?, ?)""", (columns))
and
if len(columns) == 2:
c.execute("""INSERT INTO `fb_friends`(`name`, `already_friends`) VALUES (?, ?)""", (columns))
Can any one suggest the best or most efficient way of doing this as apposed to iterating three times over the same list?
columnsto size 4 instead:columns + [None]*(4-len(columns))(columns)extremely misleading. It does nothing (you can replace(columns)withcolumnsand nothing will change), but it is similar to(columns, )(note the final comma!) which create a 1-element tuple...